使用Flume exec读取数据并读入HDFS

141 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第3天,点击查看活动详情

 官方文档:

flume.apache.org/releases/co…

实现的大致思路是使用exec读取一个命令的执行结果,并将该结果发送至HDFS中。

  • 创建conf文件
[root@hadoop01 test_conf]# pwd
/usr/local/wyh/apache-flume-1.8.0-bin/test_conf
[root@hadoop01 test_conf]# cat test-exec-hdfs.conf
myagent.sources=mysource1
myagent.channels=mychannel1
myagent.sinks=mysink1

myagent.sources.mysource1.channels=mychannel1
myagent.sinks.mysink1.channel=mychannel1

myagent.sources.mysource1.type=exec
#指定exec要监控的命令
myagent.sources.mysource1.command=tail -F /usr/local/wyh/test_data/test-exec-hdfs.txt

myagent.channels.mychannel1.type=memory

myagent.sinks.mysink1.type=hdfs
#指定将数据存在HDFS的哪个目录下,如果目录不存在会自动创建目录
myagent.sinks.mysink1.hdfs.path=hdfs://hadoop01:8020/testflume
#官方文档指出发送到hdfs的数据必须要包含一个timestamp
myagent.sinks.mysink1.hdfs.useLocalTimeStamp=true
myagent.sinks.mysink1.hdfs.writeFormat=Text
myagent.sinks.mysink1.hdfs.fileType=DataStream

  • 启动flume agent
[root@hadoop01 test_conf]# flume-ng agent -c /usr/local/wyh/apache-flume-1.8.0-bin/conf -f /usr/local/wyh/apache-flume-1.8.0-bin/test_conf/test-exec-hdfs.conf -n myagent -Dflume.root.logger=INFO,console

source和sink已启动:

 重新打开一个终端,创建数据文件:

[root@hadoop01 test_data]# cat test-exec-hdfs.txt
hello exec - hdfs flume~
[root@hadoop01 test_data]# pwd
/usr/local/wyh/test_data

回到启动flume agent的终端可以看到数据已经写入HDFS:

 也可以从目录树中看到数据文件已经存在:

 以上就是flume中使用exec监听命令执行结果并将数据写入HDFS的实现过程。