利用flume将数据采集到hdfs中。
在这里使用nginx来解决flume采集数据时的单点故障问题。
1.代码:
https://blog.csdn.net/weixin_39227099/article/details/86584711
2.启动nginx:
sbin/nginx
如下图表示启动成功
3.启动hdfs:
start-hdp.all.sh
我这里使用的是自己设置的一键启停命令,具体的可以自己配置path。
4.在flume安装目录下的【myconf】(自己创建)文件夹下,
定义nginx-hdfs.conf文件,写配置文件。
flume配置文件:
#定义agent名, source、channel、sink的名称
a1.sources = r1
a1.channels = c1
a1.sinks = k1
#具体定义source
#监听具体的class类(全类名)(自定义source)
a1.sources.r1.type = cn.edu360.flume.source.TailFileSource
#监听的文件
a1.sources.r1.filePath = /root/apps/nginx-1.14.2/logs/access.log
#记录偏移量信息
a1.sources.r1.posiFile = /root/apps/apache-flume-1.8.0-bin/posi.txt
#每隔2秒采集一次数据
a1.sources.r1.interval = 2000
#编码集
a1.sources.r1.charset = UTF-8
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
#跟上述source一样,将数据写入hdfs中
a1.sinks.k1.type = hdfs
#按日期生成目录,添加header(也可以不加)
a1.sinks.k1.hdfs.useLocalTimeStamp=true
#存储到hdfs的具体目录下
a1.sinks.k1.hdfs.path = hdfs://hdp-01:9000/flume/access-logs/%y-%m-%d
#生成文件的前缀
a1.sinks.k1.hdfs.filePrefix = events-
#生成文件的时间
a1.sinks.k1.hdfs.rollInterval=30
#不按条数生成文件
a1.sinks.k1.hdfs.rollCount=0
#生成文件的大小
a1.sinks.k1.hdfs.rollSize=104857600
#文件写入到HDFS中的存储格式
a1.sinks.k1.hdfs.fileType=DataStream
# Bind the sink to the channel
a1.sources.r1.channels=c1
a1.sinks.k1.channel = c1
5.启动flume
bin/flume-ng agent -n a1 -c conf -f myconf/nginx-logger.conf -Dflume.root.logger=INFO,console
6.查看flume配置中hdfs的保存路径下是否成功生成文件:
# hdfs dfs -ls /
查看生成文件的具体内容
# hdfs dfs -cat /flume/access-logs/19-01-22/events-.1548145362158
这里是我生成的文件,只需cat自己生成的文件即可。
测试:
在flume监听的文件 [flume配置文件说明了] 中添加数据,再次查看hdfs对应的目录下的文件查看是否可以成功的读取到即可。
注意:在flume采集数据,生成文件保存到hdfs过程中如果你设置的参数还没成立的话,生成的文件暂时是以 .tmp结尾的
即:生成文件时间不到30s,或者文件大小不到100M等等。
这里的三个条件满足一个文件即可成功生成
#生成文件的时间
a1.sinks.k1.hdfs.rollInterval=30
#不按条数生成文件
a1.sinks.k1.hdfs.rollCount=0
#生成文件的大小
a1.sinks.k1.hdfs.rollSize=104857600