目录
Flume实时监控目录下多个新文件
1)案例需求:使用Flume监听整个目录的文件,并上传至HDFS
2)需求分析:

3)实现步骤:
(1)创建配置文件flume-dir-hdfs.conf
创建一个文件
[hadoop@hadoop102 jobs]$ vim flume-dir-hdfs.conf
添加如下内容
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# Describe/configure the source
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /opt/module/flume-1.9.0/upload
a1.sources.r1.fileSuffix = .COMPLETED
a1.sources.r1.fileHeader = true
#忽略所有以.tmp结尾的文件,不上传
a1.sources.r1.ignorePattern = ([^ ]*\.tmp)
# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = hdfs://hadoop102:9820/flume/upload/%Y%m%d/%H
#上传文件的前缀
a1.sinks.k1.hdfs.filePrefix = upload-
#是否按照时间滚动文件夹
a1.sinks.k1.hdfs.round = true
#多少时间单位创建一个新的文件夹
a1.sinks.k1.hdfs.roundValue = 1
#重新定义时间单位
a1.sinks.k1.hdfs.roundUnit = hour
#是否使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
#积攒多少个Event才flush到HDFS一次
a1.sinks.k1.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a1.sinks.k1.hdfs.fileType = DataStream
#多久生成一个新的文件
a1.sinks.k1.hdfs.rollInterval = 60
#设置每个文件的滚动大小大概是128M
a1.sinks.k1.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a1.sinks.k1.hdfs.rollCount = 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
(2)启动监控文件夹命令
[hadoop@hadoop102 flume]$ flume-ng agent -c $FLUME_HOME/conf -f $FLUME_HOME/jobs/flume-dir-hdfs.conf -n a1 -Dflume.root.logger=INFO,console
说明:在使用Spooling Directory Source时,不要在监控目录中创建并持续修改文件;上传完成的文件会以.COMPLETED结尾;被监控文件夹每500毫秒扫描一次文件变动。
(3)向upload文件夹中添加文件
在/opt/module/flume目录下创建upload文件夹
[hadoop@hadoop102 flume]$ mkdir upload
向upload文件夹中添加文件
[hadoop@hadoop102 upload]$ touch hadoop.txt
[hadoop@hadoop102 upload]$ touch hadoop.tmp
[hadoop@hadoop102 upload]$ touch hadoop.log
(4)查看HDFS上的数据
