Apach Flume
Flume:
- 日志采集与汇总得工具
- 支持多种数据源(Source)不管是爬来得,还是公司买的还是收集的
- 支持多种目标源(Sink)放哪都行 可以放到hdfs里啊 sql里啊 都行,今天是放到hdfs里进行分析
1 Flume数据采集实战
1.1功能需求
1.数据采集服务器上的日志使用FLume收集(*.log文件)
2.Flume agent监听log日志文件目录
只要监听到这个目录下面有新的日志,就把这个信息推到hdfs里
3.Flume采集到的数据(log文件内容) 存储到HDFS指定目录
1.2实现步骤
-
数据采集服务可正常后台运行 (film_spider-1.0-SNAPSHOT-jar-with-dependencies.jar)
-
启动HDFS服务,并保证HDFS可读写(start-dfs.sh)
-
编写Flume agent的收集计划(包含:Source、Channel、Sink元素)
源端监测的是目录,channel是内存,目的端的hdfs
#给source(r1)、channel(c1)、sink(k1),agent(a1)命名 #agent 是a1 a1.sources = r1 a1.sinks = k1 a1.channels = c1 #配置采集源source a1.sources.r1.type = taildir a1.sources.r1.filegroups = f1 #检测的源地址路径 a1.sources.r1.filegroups.f1=/opt/data/1_22/data/collect/.*\.log a1.sources.r1.positionFile = /opt/data/1_22/logs/flume_postion.json #配置采集后存储的目标:sink a1.sinks.k1.type= hdfs a1.sinks.k1.hdfs.path=hdfs://hadoop01:9000/flume/%Y-%m-%d a1.sinks.k1.hdfs.rollInterval = 30 a1.sinks.k1.hdfs.rollSize=1048576 a1.sinks.k1.hdfs.rollCount=0 a1.sinks.k1.hdfs.filePrefix=log_file_%H a1.sinks.k1.hdfs.fileSuffix=.log a1.sinks.k1.hdfs.fileType=DataStream a1.sinks.k1.hdfs.useLocalTimeStamp = true #配置channel a1.channels.c1.type = memory a1.channels.c1.capacity =1000 a1.channels.c1.transactionCapacity =100 #给source和sink 配置channel a1.sources.r1.channels = c1 a1.sinks.k1.channel =c1 -
编写shell脚本实现
可执行文件是绿的
shell脚本的名字:start_flume_to_hdfs.sh
#!/bin/bash $FLUME_HOME/bin/flume-ng agent --conf $FLUME_HOME/conf -f /opt/data/1_22/flume-taildir-memory-hdfs.conf -Dflume.root.logger=INFO,console -n a1 #agent的名字修改shell脚本的可执行权限
[hadoop@hadoop01 1_22]$ chmod +x start_flume_to_hdfs.sh执行shell脚本
[hadoop@hadoop01 1_22]$ nohup ./start_flume_to_hdfs.sh & #./代表当前路径 [2] 2680 [hadoop@hadoop01 1_22]$ nohup: 忽略输入并把输出追加到'nohup.out' [hadoop@hadoop01 1_22]$执行完shell脚本的效果:hdfs的 /flume下回自动生成2021-01-22的目录。
就是把爬虫爬到的存在本地数据导入到hdfs里
tail -f 实时监测日志
[hadoop@hadoop01 1_22]$ tail -f nohup.out
每隔30s会把源端的数据导入hdfs相应目录生成对应文件,或者说文件大小达到了1M也会生成对应文件