filnk入门

编写filnk api调用代码

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
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;

public class test {
    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> {
        public void flatMap(String sentence, Collector> out) throws Exception {
            for (String word: sentence.split(" ")) {
                out.collect(new Tuple2(word, 1));
            }
        }
    }
}

pom文件:



    4.0.0

    test1
    test
    1.0-SNAPSHOT


    

        

            org.apache.flink

            flink-java

            1.5.1

        

        

            org.apache.flink

            flink-streaming-java_2.11

            1.5.1

        

        

            org.apache.flink

            flink-clients_2.11

            1.5.1

        
        

            org.apache.flink

            flink-metrics

            1.5.1

        



    


 

程序监听本地9999端口,并接收单词并统计5秒之内接收到的不同单词的数量,若是收到一个句子,则以空格分隔:如“gg hh jj”, 则拆分为三个单词“gg”,“hh”,“jj”。

 

测试时,现在本地启动socket服务端(nc.exe), nc -L -p 9999。  然后run启动flink程序链接socket。  之后可通过socket服务端发送单词或句子,又flink统计并输出。效果如下:

filnk入门_第1张图片

你可能感兴趣的:(flink)