Flink学习(一):SocketWindowWordCount示例

 

参考资料:官方文档 & 官方示例代码

 

首先是环境配置,很简单,下载下来解压就可以。

然后运行bin/start-cluster.sh启动Flink,虽然脚本名字是cluster,不过默认配置是启动的本地模式。启动后,可以在浏览器输入localhost:8081进入Dashboard:

Flink学习(一):SocketWindowWordCount示例_第1张图片

接下来就是按照给定的代码编写示例程序并运行,不过程序example目录已经有打包好的示例程序,可以拿来直接运行。SocketWindowWordCount源码如下:

public class SocketWindowWordCount {

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

        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // get input data by connecting to the socket
        DataStream text = env.socketTextStream("localhost", 9000, "\n");

        // parse the data, group it, window it, and aggregate the counts
        DataStream windowCounts = text
            .flatMap(new FlatMapFunction() {
                @Override
                public void flatMap(String value, Collector out) {
                    for (String word : value.split("\\s")) {
                        out.collect(new WordWithCount(word, 1L));
                    }
                }
            })
            .keyBy("word")
            .timeWindow(Time.seconds(5), Time.seconds(1))
            .reduce(new ReduceFunction() {
                @Override
                public WordWithCount reduce(WordWithCount a, WordWithCount b) {
                    return new WordWithCount(a.word, a.count + b.count);
                }
            });

        // print the results with a single thread, rather than in parallel
        windowCounts.print().setParallelism(1);

        env.execute("Socket Window WordCount");
    }

    // Data type for words with count
    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 word + " : " + count;
        }
    }
}

编写Flink程序需要引入相关依赖(以Flink 1.9,基于Scala 2.12为例):

    
        
            org.apache.flink
            flink-java
            1.9.1
            provided
        
        
            org.apache.flink
            flink-streaming-java_2.12
            1.9.1
            provided
        
    

然后就是打包,不过还需要配置打包插件,否则会提示找不到主类(配置抄自官方代码):


        
            
            
                org.apache.maven.plugins
                maven-jar-plugin
                2.4
                
                    
                    
                        default
                        package
                        
                            test-jar
                        
                    

                    
                        SocketWindowWordCount
                        package
                        
                            jar
                        
                        
                            SocketWindowWordCount

                            
                                
                                    你的程序路径.WordCount
                                
                            

                            
                                你的程序路径/WordCount.class
                                你的程序路径/WordCount$*.class
                            
                        
                    
                
            
        
    

流计算代码必须位于streaming目录下,批处理代码必须位于batch目录下。

然后运行打好包的flinkdemo-1.0-RELEASE-SocketWindowWordCount.jar,这里我复制到了Flink根目录下:

bin/flink run flinkdemo-1.0-RELEASE-SocketWindowWordCount.jar

同时在另一个Terminal中输入 nc -l 9000,然后随便输入一些词,例如:hello world flink hello flink,此时可以观察Dashboard,看到新任务正在执行:

在新的Terminal窗口中,观察Flink目录下,log/flink-你的用户名-taskexecutor-0-你的机器名.out,有如下输出:

root@Yhc-Surface:~/flink-1.9.1/log# tail -f flink-root-taskexecutor-0-Yhc-Surface.out
flink : 2
flink : 2
world : 1
hello : 2
world : 1
flink : 2
hello : 2
hello : 2
world : 1
flink : 2

显示hello、flink都出现了两次,world出现一次,和输入符合。

在nc -l 9000窗口使用Ctrl+C后,示例程序也会退出,且Dashboard中,该程序的状态会变成FINISHED。

也可以在Dashboard左侧工具栏最下方的“Submit New Job”中上传Jar包运行。

你可能感兴趣的:(Flink)