第一个Flink程序 词频统计

  • maven创建 Flink 应用程序,通过端口获取实时输入的字符串,利用Flink统计单词频率并打印出来

第一个Flink程序 词频统计_第1张图片

public static void main(String[] args) throws Exception {

    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    DataStream> dataStream = env
            .socketTextStream("localhost", 9999)
            .flatMap(new Splitter())
            .keyBy(0)
            .timeWindow(Time.seconds(5))
            .sum(1);

    dataStream.print();

    env.execute("Window WordCount");
}

public static class Splitter implements FlatMapFunction> {
    @Override
    public void flatMap(String sentence, Collector> out) throws Exception {
        for (String word: sentence.split(" ")) {
            out.collect(new Tuple2(word, 1));
        }
    }
}
necat 操作命令

linux
nc -lk 8080

wwindow 
nc -i -p 8080

你可能感兴趣的:(大数据)