Flume 之 Spooling Directory source、memory channel、hdfs sink
2.3 实时监控目录下多个新文件
1)案例需求:使用 Flume 监听整个目录的文件,并上传至 HDFS
2)需求分析:
3)实现步骤:
(1)创建配置文件 files-flume-hdfs.conf
- 创建一个文件
[xiaoxq@hadoop105 jobs]$ vim files-flume-hdfs.conf
- 添加如下内容
a3.sources = r3
a3.sinks = k3
a3.channels = c3
# Describe/configure the source
a3.sources.r3.type = spooldir
a3.sources.r3.spoolDir = /opt/module/flume-1.9.0/upload
a3.sources.r3.fileSuffix = .COMPLETED
#忽略所有以.tmp结尾的文件,不上传
a3.sources.r3.ignorePattern = ([^ ]*\.tmp)
# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://hadoop105:9820/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
(2)启动监控文件夹命令
[xiaoxq@hadoop105 flume-1.9.0]$ bin/flume-ng agent --conf conf/ --name a3 --conf-file jobs/files-flume-hdfs.conf
- 说明:在使用 Spooling Directory Source 时,不要在监控目录中创建并持续修改文件;上传完成的文件会以.COMPLETED 结尾;被监控文件夹每500毫秒扫描一次文件变动。
(3)向upload文件夹中添加文件
- 在/opt/module/flume-1.9.0目录下创建upload文件夹
[xiaoxq@hadoop105 upload]$ pwd
/opt/module/flume-1.9.0
[xiaoxq@hadoop105 flume-1.9.0]$ mkdir upload
[xiaoxq@hadoop105 flume-1.9.0]$ cd upload/
- 向 upload 文件夹中添加文件
[xiaoxq@hadoop105 upload]$ touch wukong.txt
[xiaoxq@hadoop105 upload]$ touch bajie.tmp
[xiaoxq@hadoop105 upload]$ touch tangsheng.log
[xiaoxq@hadoop105 upload]$ ll
总用量 0
-rw-rw-r--. 1 xiaoxq xiaoxq 0 8月 7 20:11 bajie.tmp
-rw-rw-r--. 1 xiaoxq xiaoxq 0 8月 7 20:11 tangsheng.log
-rw-rw-r--. 1 xiaoxq xiaoxq 0 8月 7 20:11 wukong.txt
(4)查看HDFS上的数据
(5)等待1s,再次查询 upload 文件夹
[xiaoxq@hadoop105 upload]$ pwd
/opt/module/flume-1.9.0/upload
[xiaoxq@hadoop105 upload]$ ll
总用量 0
-rw-rw-r--. 1 xiaoxq xiaoxq 0 8月 7 20:11 bajie.tmp
-rw-rw-r--. 1 xiaoxq xiaoxq 0 8月 7 20:11 tangsheng.log.COMPLETED
-rw-rw-r--. 1 xiaoxq xiaoxq 0 8月 7 20:11 wukong.txt.COMPLETED
-
小结:
-
spooling dir 1.这个 source 是拿来上传已经完整文件 2.这个 source 能够去每隔 500ms 去扫描一次指定的文件夹 3.不要在指定的文件夹 持续修改文件 会导致重复上传 4.不要传同名文件两次,会导致这个agent挂掉 5.不要传指定文件后缀名(.COMPLETED)的文件(会默认这个文件已经扫描过) 6.先上传文件后改名