入门级程序
public class SocketWindowWordCountJava {
public static void main(String[] args) throws Exception {
//获取 flink 的运行环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
String host="localhost";
int port=9000;
String delimiter="\n";
//
DataStreamSource<String> text = env.socketTextStream(host, port, delimiter);
SingleOutputStreamOperator<WordCount> wcs = text.flatMap(new FlatMapFunction<String, WordCount>() {
@Override
public void flatMap(String value, Collector<WordCount> out) throws Exception {
String[] vs = value.split("\\s");
for (String word : vs) {
out.collect(new WordCount(word, 1L));
}
}
}).keyBy("word")
.timeWindow(Time.seconds(2), Time.seconds(1))
.sum("count");
wcs.print().s