一、任务概况
大概模仿一下流数据的产生(通过脚本定时生成)、传递(通过FileBeat监控double11.log文件并实时传送给kafka)、处理(通过kafka传递过来的数据交给Flink进行统计)、存储(用java实现连接Redis并存储)。
模仿的事例过程是很简化的天猫双11实时交易的流程。
二、具体实现
1.模拟生产实时流数据
利用Linux shell自动化模拟每秒钟产生一条交易额数据,数据内容为用户id,购买商品的付款金额,用户所在城市及所购买的商品 。
double11.sh代码如下:
#!/bin/bash
i=1
for i in $(seq 1 60)
do
customernum=`openssl rand -base64 8 | cksum | cut -c1-8`
pricenum=`openssl rand -base64 8 | cksum | cut -c1-4`
citynum=`openssl rand -base64 8 | cksum | cut -c1-2`
itemnum=`openssl rand -base64 8 | cksum | cut -c1-6`
echo "customer"$customernum","$pricenum",""city"$citynum",""item"$itemnum >> /usr/local/data2/double11/double11.log
sleep 1
done
在shell中执行
生成数据保存到double11.log文件中
利用 Linux crontab 每分钟执行一次这个脚本(参考链接: https://blog.csdn.net/qq_40374604/article/details/86540393 ),便可以模拟生成流数据。
2. Filebeat实时监控double11.log产生的每条交易额记录,将记录实时流向到Kafka的topic 。
FileBeat的安装参考链接: https://blog.csdn.net/jeikerxiao/article/details/84841792
主要做两件事情:
-
配置FileBeat读入文件的路径,修改paths,如下配置能读取/usr/local/data2/double11/下所有以.log结尾的文件。
-
配置FileBeat输出,hosts是Kafka主机IP,topic是传递的话题,还有很多参数感兴趣的话可以自己查阅。
在shell中执行./filebeat -e -c filebeat.yml命令来启动Filebeat,显示的东西是很乱,但这就是正常启动的样子。
3.Flink对Kafka传入的数据进行处理。
这部分是核心工作,利用模拟生成的消费者信息可以做很多事情,在这里就简单做了实时总交易额客户端消费kafka数据和根据城市实时总交易额客户端消费kafka数据这两件事情。
我这里编程工具为sts,其实Idea和eclipse都可以,看个人喜好。使用Flink Java API,我对这些API的具体工作过程不是很理解,在网上找了很多资料,勉强知道了这些API可以怎么使用,所以代码中的注释不是很详细,有兴趣的话可以自己去查阅。
需要导入的maven依赖 :
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.flink/flink-clients -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients_2.11</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.flink/flink-streaming-java -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.11</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.flink/flink-java -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-kafka-0.8_2.11<