简单的流式处理代码
package com.heiheihei.flink;
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;
/**
* @author 嘿嘿嘿1212
* @version 1.0
* @date 2020/3/31 17:22
*/
public class SocketWindowWordCount {
public static void main(String[] args) throws Exception {
//指定默认端口
int port = 8081;
try {
//可进行指定端口
final ParameterTool params = ParameterTool.fromArgs(args);
port = params.getInt("port");
} catch (Exception e) {
System.err.println("No port specified. Please run 'SocketWindowWordCount --port");
}
//获取环境
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//获取数据
DataStream text = env.socketTextStream("localhost", port, "\n");
//编写算子
DataStream<WordWithCount> windowCounts = text.flatMap((FlatMapFunction<String, WordWithCount>) (s, collector) -> {
for (String word : s.split(" ")) {
collector.collect(new WordWithCount(word, 1L));
}
});
//进行窗口时间等指定
windowCounts.keyBy("word")
.timeWindow(Time.seconds(5), Time.seconds(1))
.reduce(new ReduceFunction<WordWithCount>() {
@Override
public WordWithCount reduce(WordWithCount wordWithCount, WordWithCount t1) throws Exception {
return new WordWithCount(wordWithCount.word, wordWithCount.count + t1.count);
}
});
windowCounts.print().setParallelism(1);
env.execute("Soket Window WordCount");
}
public static 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 "WordWithCount{" +
"word='" + word + '\'' +
", count=" + count +
'}';
}
}
}