Flink简单运用Demo

版权声明:欢迎分享转载 我可能会失败,但我不会一直失败 https://blog.csdn.net/u012637358/article/details/88667919

1、构建maven工程

 <dependencies>
    <dependency>
  <groupId>org.apache.flink</groupId>
  <artifactId>flink-java</artifactId>
  <version>1.7.2</version>
</dependency>
<dependency>
  <groupId>org.apache.flink</groupId>
  <artifactId>flink-streaming-java_2.11</artifactId>
  <version>1.7.2</version>
</dependency>
<dependency>
  <groupId>org.apache.flink</groupId>
  <artifactId>flink-clients_2.11</artifactId>
  <version>1.7.2</version>
</dependency>

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 <port>'");
		return ;
	}
	//获取执行环节
	final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
	
	//获取连接socket输入数据
	DataStream<String> text = env.socketTextStream("node3.kg.cn", port,"\n");
	
	//解析数据、对数据进行分组、窗口函数和统计个数
	
	DataStream<WordWithCount> windowCounts =text.flatMap(new FlatMapFunction<String, WordWithCount>() {

		private static final long serialVersionUID = 6800597108091365154L;

		public void flatMap(String value, Collector<WordWithCount> 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<WordWithCount>() {
				
				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、本地启动工程

在这里插入图片描述
在这里插入图片描述
'run’运行

3、运行nc

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

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u012637358/article/details/88667919