使用Flume作为Spark Streaming数据源
一、实验目的
(1)通过实验学习日志采集工具Flume的安装和使用方法;
(2)掌握采用Flume作为Spark Streaming数据源的编程方法。
二、实验平台
操作系统:CentOS
Spark版本:2.4.7
Flume版本:1.7.0
三、实验内容和要求
1.安装Flume
解压安装包
tar -zxvf apache-flume-1.7.0-bin.tar.gz -C /opt
ln -sv apache-flume-1.7.0-bin.tar.gz apache-flume
配置环境变量
vi ~/.bash_profile
修改内容如下:
export FLUME_HOME=/opt/apache-flume-1.7.0-bin
export PATH=$PATH:$FLUME_HOME/bin
source ~/.bash_profile
配置flume-env.sh文件
cd apache-flume-1.7.0-bin/conf/
cp flume-env.sh.template flume-env.sh
vi flume-env.sh
修改内容如下:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el8_2.x86_64
export HADOOP_HOME=/opt/hadoop-2.7.4
版本验证
flume-ng version
2. 使用netcat数据源测试Flume
创建一个Flume配置文件
cd apache-flume-1.7.0-bin
mkdir example
cp conf/flume-conf.properties.template example/netcat.conf
配置netcat.conf用于实时获取另一终端输入的数据
vi example/netcat.conf
修改内容如下:
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444
# Describe the sink
a1.sinks.k1.type = logger
# Use a channel that buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
在一个Linux终端(这里称为“Flume终端”)中,启动Flume
flume-ng agent -c conf -f example/netcat.conf -n a1 -Dflume.root.logger=INFO,console
在另一个终端(这里称为“Telnet终端”)中,输入命令“telnet localhost 44444”,如果报错:
bash: telnet: command not found
说明容器中没有telnet,需要下载:
yum list telnet* #列出telnet相关的安装包
yum install telnet-server #安装telnet服务
yum install telnet.* #安装telnet客户端
输入 "ss -tnl"查看:
此时再次输入命令:telnet localhost 44444
然后,在Telnet终端中输入任何字符,让这些字符可以顺利地在Flume终端中显示出来。
Telnet终端
Flume终端
成功!
3.使用Flume作为Spark Streaming数据源
Flume是非常流行的日志采集系统,可以作为Spark Streaming的高级数据源。请把Flume Source设置为netcat类型,从终端上不断给Flume Source发送各种消息,Flume把消息汇集到Sink,这里把Sink类型设置为avro,由Sink把消息推送给Spark Streaming,由自己编写的Spark Streaming应用程序对消息进行处理。
配置Flume数据源
执行如下命令新建一个Flume配置文件flume-to-spark.conf:
cd apache-flume-1.7.0-bin
cd conf
vi flume-to-spark.conf
内容如下:
#flume-to-spark.conf: A single-node Flume configuration
# Name the components on this agent
a2.sources = r2
a2.sinks = k2
a2.channels = c2
# Describe/configure the source
a2.sources.r2.type = netcat
a2.sources.r2.bind = localhost