Flink简单运用Demo

1、构建maven工程

 
    
  org.apache.flink
  flink-java
  1.7.2


  org.apache.flink
  flink-streaming-java_2.11
  1.7.2


  org.apache.flink
  flink-clients_2.11
  1.7.2

SocketWindowWordCount.java

package com.jimu.flink_example;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

/**   
 * @ClassName:  SocketWindowWordCount   
 * @Description:TODO(flink单词个数统计)   
 * @author: Jimu 
 * @email:  [email protected] 
 * @date:   2019年3月19日 下午3:18:03   
 *     
 * @Copyright: 2019 www.maker-win.net Inc. All rights reserved. 
 *  
 */
public class SocketWindowWordCount {
  public static void main(String[] args) throws Exception{
	 //连接端口号
	final int port;
	try {
		final ParameterTool params = ParameterTool.fromArgs(args);
		port = params.getInt("port");
	} catch (Exception e) {
		System.out.println("No port specified. Please run 'SocketWindowWordCount --port '");
		return ;
	}
	//获取执行环节
	final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	
	//获取连接socket输入数据
	DataStream text = env.socketTextStream("node3.kg.cn", port,"\n");
	
	//解析数据、对数据进行分组、窗口函数和统计个数
	
	DataStream windowCounts =text.flatMap(new FlatMapFunction() {

		private static final long serialVersionUID = 6800597108091365154L;

		public void flatMap(String value, Collector out) throws Exception {
			 for(String word:value.split("//s")) {
				 out.collect(new WordWithCount(word, 1));
			 }
		}
	})
			.keyBy("word")
			.timeWindow(Time.seconds(5),Time.seconds(1))
			.reduce(new ReduceFunction() {
				
				public WordWithCount reduce(WordWithCount value1, WordWithCount value2) throws Exception {
					
					return new WordWithCount(value1.word,value1.count+value2.count);
				}
			});
	windowCounts.print().setParallelism(1);
	
	env.execute("Socket Window WordCount");
	
 }
  
}

WordWithCount.java

package com.jimu.flink_example;

/**   
 * @ClassName:  WordWithCount   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: Jimu 
 * @email:  [email protected] 
 * @date:   2019年3月19日 下午3:26:52   
 *     
 * @Copyright: 2019 www.maker-win.net Inc. All rights reserved. 
 *  
 */
public class WordWithCount {
	public String word;
    public long count;

    public WordWithCount() {}

    public WordWithCount(String word, long count) {
        this.word = word;
        this.count = count;
    }

    @Override
    public String toString() {
        return word + " : " + count;
    }
}

2、本地启动工程

Flink简单运用Demo_第1张图片
Flink简单运用Demo_第2张图片
'run’运行

3、运行nc

[root@node3 /]# nc -l 9000
hadoop
flink
java
fling
flink'
java
java
scal
ca
ca

Flink简单运用Demo_第3张图片

你可能感兴趣的:(Flink)