
上QQ阅读APP看书,第一时间看更新
2.6 入门案例
如前所述,Flink的任务可以运行在各种模式下,每种模式都有不同的应用场景,那么接下来开始学习Flink编程,主要包括批量数据处理和实时数据处理。图2-3所示为对Flink编程进行的分类。

●图2-3 Flink编程分类
2.6.1 实时处理程序实现
编写Flink代码统计Socket中接收到的每个单词出现的次数。这里使用IntelliJ IDEA开发工具,在其中创建Maven的Java工程,然后在工程的pom.xml中添加jar包的坐标依赖。
(1)创建Maven工程,导入jar包



(2)编写Flink代码统计Socket中的单词数量
接收Socket单词数据,然后对数据进行统计。代码如下。

(3)打包上传到服务器
将程序打包后上传到node01服务器,然后在各种模式下运行程序。
(4)standalone模式下运行程序
在node01上执行以下命令启动Flink集群。

启动node01的Socket服务。

将打包好的jar包上传到node01服务器的/kkb路径下,才能提交任务。注意,打包前需要在pom.xml中添加打包插件,且集群已有代码需要将“scope”设置为“provided”。在pom.xml中将关于Flink的jar包“scope”设置为“provided”,如图2-4所示。

●图2-4 打包级别调整
jar-with-dependencies的jar包上传到/kkb路径后,需要的jar包如图2-5所示。

●图2-5 打包之后的jar包
在node01上执行以下命令提交任务。

查看运行结果。

运行结果中有很多文件,选择查看有内容的文件即可,如图2-6所示。

●图2-6 查看输出日志
注意:
结果保存在以“.out”结尾的文件中,哪个文件中有数据就查看哪个文件。
2.6.2 离线批量处理程序实现
本节完成count.txt文件的处理,实现单词计数。

