05 Flume入门案例之实时监控目录下多个新文件

案例需求:

使用 Flume 监听整个目录的文件,并上传至 HDFS

需求分析:

 

实现步骤:

创建配置文件 flume-dir-hdfs.conf

vim flume-dir-hdfs.conf

编写配置文件

a3.sources = r3
a3.sinks = k3
a3.channels = c3

# Describe/configure the source
a3.sources.r3.type = spooldir
a3.sources.r3.spoolDir = /export/servers/flume/upload
# 定义文件上传完的添加的文件后缀,用以区别上传过与未上传过的文件
a3.sources.r3.fileSuffix = .COMPLETED
# 是否含有文件头
a3.sources.r3.fileHeader = true
# 忽略所有以.tmp 结尾的文件,不上传
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)

# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path =hdfs://hadoop101:8020/flume/upload/%Y%m%d/%H
# 上传文件的前缀
a3.sinks.k3.hdfs.filePrefix = upload-
# 是否按照时间滚动文件夹
a3.sinks.k3.hdfs.round = true
# 多少时间单位创建一个新的文件夹
a3.sinks.k3.hdfs.roundValue =  1 
#重新定义时间单位
a3.sinks.k3.hdfs.roundUnit = hour
#是否使用本地时间戳
a3.sinks.k3.hdfs.useLocalTimeStamp = true
#积攒多少个Event才flush到HDFS 一次
a3.sinks.k3.hdfs.batchSize = 100
#设置文件类型,可支持压缩
a3.sinks.k3.hdfs.fileType = DataStream
#多久生成一个新的文件
a3.sinks.k3.hdfs.rollInterval = 60
#设置每个文件的滚动大小大概是 128M
a3.sinks.k3.hdfs.rollSize = 134217700
#文件的滚动与Event 数量无关
a3.sinks.k3.hdfs.rollCount = 0

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

# Bind the source and sink to the channel
a3.sources.r3.channels = c3
a3.sinks.k3.channel = c3

创建upload文件夹

mkdir upload

启动flume

bin/flume-ng agent -c conf/ -n a3 -f job/flume-dir-hdfs.conf

注意:在使用 Spooling Directory Source 时,不要在监控目录中创建并持续修改文件;上传完成的文件会以.COMPLETED 结尾;被监控文件夹每 500 毫秒扫描一次文件变动

测试

创建1.txt文件

touch 1.txt

移动到upload

mv 1.txt upload/

查看upload文件夹

ll upload/

查看hdfs

 

创建1.tmp

touch 1.tmp

移动到upload文件夹

mv 1.tmp upload/

查看upload文件夹

ll upload/
查看hdfs

 

创建1.COMPLETED 

touch 1.COMPLETED

移动到upload文件夹

mv 1.COMPLETED upload/

查看upload文件夹

ll upload/
 
 
查看hdfs

 

再次移动1.txt到upload 

touch 1.txt

mv 1.txt upload/

 查看flume的日志信息

报错:

查看hdfs  

 

 报错原因:这是因为监管的目录下不允许出现两个相同名字的文件,再次移动1.txt到upload目录中,首先并没有文件与它同名,但是当它上传完后再加上.COMPLETED后缀就 出现同名的情况,因此报错

猜你喜欢

转载自blog.csdn.net/m0_55868614/article/details/120754486
05