Flume使用详解
2018-10-08 本文已影响0人
小猪Harry
使用Flume监听日志变化
进入到flume的conf目录下,创建文件,进行配置
[root@mini1 ~]# cd apps/apache-flume-1.6.0-bin/conf/
[root@mini1 conf]# ll
总用量 28
-rw-r--r--. 1 501 games 1661 5月 9 2015 flume-conf.properties.template
-rw-r--r--. 1 501 games 1110 5月 9 2015 flume-env.ps1.template
-rw-r--r--. 1 501 games 1214 5月 9 2015 flume-env.sh.template
-rw-r--r--. 1 501 games 3107 5月 9 2015 log4j.properties
-rw-r--r--. 1 root root 487 10月 19 14:34 netcat-logger.conf
-rw-r--r--. 1 root root 507 10月 19 01:57 spool-logger.conf
-rw-r--r--. 1 root root 1271 10月 19 15:11 tail-hdfs.conf
[root@mini1 conf]# vi netcat-logger.conf
# example.conf: A single-node Flume configuration
# Name the components on this agent
#给那三个组件取个名字
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
## exec表示flume回去调用给的命令,然后从给的命令的结果中去拿数据
a1.sources.r1.type = exec
## 使用tail这个命令来读数据
a1.sources.r1.command = tail -F /opt/test.log
a1.sources.r1.channels = c1
# Describe the sink 日志下沉到log4j,打印在屏幕上
a1.sinks.k1.type = logger
# Use a channel which buffers events in memory
#下沉的时候是一批一批的, 下沉的时候是一个个event
Channel参数解释:
#capacity:默认该通道中最大的可以存储的event数量 1000条数据(1000个event,source拿到的数据是封装成event事件的)
#trasactionCapacity:每次最大可以从source中拿到或者送到sink中的event数量
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
配置好了就可以启动了
[root@mini1 apache-flume-1.6.0-bin]# bin/flume-ng agent --conf conf --conf-file conf/netcat-logger.conf --name a1 -Dflume.root.logger=INFO,console
Warning: JAVA_HOME is not set!
...
2017-10-20 05:00:13,317 (conf-file-poller-0) [INFO - org.apache.flume.node.Application.startAllComponents(Application.java:173)] Starting Sink k1
2017-10-20 05:00:13,318 (conf-file-poller-0) [INFO - org.apache.flume.node.Application.startAllComponents(Application.java:184)] Starting Source r1
2017-10-20 05:00:13,320 (lifecycleSupervisor-1-3) [INFO - org.apache.flume.source.NetcatSource.start(NetcatSource.java:150)] Source starting
2017-10-20 05:00:13,350 (lifecycleSupervisor-1-3) [INFO - org.apache.flume.source.NetcatSource.start(NetcatSource.java:164)] Created serverSocket:sun.nio.ch.ServerSocketChannelImpl[/192.168.25.127:44444]
通过写一个死循环往test.log中写数据的方式模式日志文件增长
编写shell脚本,模拟日志增长变化。
[root@hadoop1 flumedata]# cd /home/tuzq/software/flumedata
[root@hadoop1 flumedata]# while true
>do
> date >> test.log
> sleep 2
> done
查看日志变化
[root@hadoop1 ~]# cd /home/tuzq/software/flumedata/
[root@hadoop1 flumedata]# ls
access.log error.log test.log
[root@hadoop1 flumedata]# tail -f test.log
2017年 06月 13日 星期二 22:02:22 CST
2017年 06月 13日 星期二 22:02:24 CST
2017年 06月 13日 星期二 22:02:26 CST
2017年 06月 13日 星期二 22:02:28 CST
2017年 06月 13日 星期二 22:02:30 CST
2017年 06月 13日 星期二 22:02:32 CST
通过上面的文件,可以看到test.log在不停的追加数据。
Flume的使用(三)
采集数据到hdfs
这里要添加的配置文件里面的采集源和下沉地就都有变化了。
[root@mini1 conf]# vi tail-hdfs.conf
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
#exec 指的是命令
# Describe/configure the source
a1.sources.r1.type = exec
#F根据文件名追中, f根据文件的nodeid追中
a1.sources.r1.command = tail -F /opt/test.log
a1.sources.r1.channels = c1
# Describe the sink
#下沉目标
a1.sinks.k1.type=hdfs
a1.sinks.k1.channel=c1
a1.sinks.k1.hdfs.useLocalTimeStamp=true
a1.sinks.k1.hdfs.path=/flume/testout9/
a1.sinks.k1.hdfs.filePrefix=cmcc
a1.sinks.k1.hdfs.minBlockReplicas=1
a1.sinks.k1.hdfs.fileType=DataStream
a1.sinks.k1.hdfs.writeFormat=Text
a1.sinks.k1.hdfs.rollInterval=0
a1.sinks.k1.hdfs.rollSize=10240
a1.sinks.k1.hdfs.rollCount=0
a1.sinks.k1.hdfs.idleTimeout=0
# Use a channel which 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
flume+kafka
spooldir.sources= eventDir
spooldir.channels= memoryChannel
spooldir.sinks= eventHDFS
spooldir.channels.memoryChannel.type= memory
spooldir.channels.memoryChannel.capacity= 10000
spooldir.channels.memoryChannel.transactioncapacity= 1000000
spooldir.sources.eventDir.type=exec
spooldir.sources.eventDir.command=tail -F /opt/log.txt
spooldir.sinks.eventHDFS.type = org.apache.flume.sink.kafka.KafkaSink
spooldir.sinks.eventHDFS.topic = orderMq
spooldir.sinks.eventHDFS.brokerList = hadoop01:9092,hadoop02:9092,hadoop03:9092
spooldir.sources.eventDir.channels= memoryChannel
spooldir.sinks.eventHDFS.channel= memoryChannel
参考
https://blog.csdn.net/zengmingen/article/details/65444823