Flume-实时监控单个文件追加内容(Exec Source+Logger Sink或HDFS Sink)

实时监控单个文件追加内容

案例需求:实时监控日志文件内容,并上传到 HDFS 中

使用exec source监控本地文件修改;将任务,分成两个部分,先将文件修改的内容输出到控制台打印,然后再输出到HDFS上

监控文件内容输出到控制台

配置文件内容如下:

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -f /home/bd/tmp/log

# Describe the sink
a1.sinks.k1.type = logger

# 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

运行命令如下:

bin/flume-ng agent -c conf/ -f job/file-flume-logger.conf -n a1 -Dflume.root.logger=INFO,console

运行起来之后,往文件中写入几行数据,查看运行情况:

在这里插入图片描述

确实实时将新写入的数据打印到了控制台上。

监控文件内容输出到HDFS

Flume要想将数据输出到 HDFS,必须持有Hadoop相关jar包

commons-configuration-1.6.jar
commons-io-2.4.jar
htrace-core-3.1.0-incubating.jar
hadoop-common-2.7.2.jar
hadoop-auth-2.7.2.jar
hadoop-hdfs-2.7.2.jar

## 将以上jar包拷贝到flume的lib文件夹下

配置文件中的其他配置可以通过http://flume.apache.org/releases/content/1.7.0/FlumeUserGuide.html进行查看,HDFS是实践中比较常用的sink。

非常常用的配置如下

namedefaultdescription
hdfs.rollInterval30多少秒之后进行文件的滚动,配置成0时不通过时间滚动
hdfs.rollSize1024文件达到多大之后进行文件滚动,单位为byte,最好配置成比HDFS的块小一点点(保证一个文件不会超过一个块,如配置成127 * 1024 * 1024),配置成0时不通过大小滚动
hdfs.rollCount10事件达到多少之后进行文件滚动,配置成0是不通过事件数进行滚动
hdfs.roundfalse是否进行文件夹滚动,false为关闭,true为开启;配置文件夹滚动时,保存文件的文件夹名称需要与时间进行关联如 /test%Y%m%d
hdfs.roundValue1文件夹滚动的value与单位共同作用
hdfs.roundUnitsecond单位有second, minute or hour.
hdfs.useLocalTimeStampfalse是否使用本地时间戳,使用round的时候需要用到时间,因此有配置round时也需要将其配置成true

配置文件内容如下:

# Name the components on this agent
a2.sources = r2
a2.sinks = k2
a2.channels = c2

# Describe/configure the source
a2.sources.r2.type = exec
a2.sources.r2.command = tail -F /home/bd/tmp/log

# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop113:9000/flume/%Y%m%d/%H

#上传文件的前缀
a2.sinks.k2.hdfs.filePrefix = logs-

#是否按照时间滚动文件夹
a2.sinks.k2.hdfs.round = true
#多少时间单位创建一个新的文件夹
a2.sinks.k2.hdfs.roundValue = 1
#重新定义时间单位
a2.sinks.k2.hdfs.roundUnit = hour

#是否使用本地时间戳
a2.sinks.k2.hdfs.useLocalTimeStamp = true

#积攒多少个 Event 才 flush 到 HDFS 一次,到了一定时间了也会去flush
a2.sinks.k2.hdfs.batchSize = 1000

#设置文件类型,可支持压缩
a2.sinks.k2.hdfs.fileType = DataStream

#多久生成一个新的文件,60s
a2.sinks.k2.hdfs.rollInterval = 60
#设置每个文件的滚动大小
a2.sinks.k2.hdfs.rollSize = 134217700
#文件的滚动与 Event 数量无关
a2.sinks.k2.hdfs.rollCount = 0

# Use a channel which buffers events in memory
a2.channels.c2.type = memory
a2.channels.c2.capacity = 1000
a2.channels.c2.transactionCapacity = 100

# Bind the source and sink to the channel
a2.sources.r2.channels = c2
a2.sinks.k2.channel = c2

运行起来之后,文件如下,当前正在被写都以tmp后缀的

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值