Flume 案例实操 - 实时读取目录文件到HDFS

墨蓝 2022-01-30 11:37 428阅读 0赞

使用flume监听整个目录的文件

需求分析

  1. 创建符合条件的flume配置文件
  2. 执行配置文件,开启监控
  3. 向目录中添加文件,被监控的目录/opt/module/flume/upload
  4. 查看HDFS上数据
  5. 查看/opt/module/flume/upload目录中上传的文件是否已经标记为.COMPLETED结尾;.tmp后缀结尾文件没有上传。

实现步骤

创建配置文件

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

  1. cd job
  2. vim flume-dir-hdfs.conf

添加如下内容

  1. a3.sources = r3
  2. a3.sinks = k3
  3. a3.channels = c3
  4. # Describe/configure the source
  5. a3.sources.r3.type = spooldir
  6. a3.sources.r3.spoolDir = /opt/module/flume-1.7.0/upload
  7. a3.sources.r3.fileSuffix = .COMPLETED
  8. a3.sources.r3.fileHeader = true
  9. #忽略所有以.tmp结尾的文件,不上传
  10. a3.sources.r3.ignorePattern = ([^ ]*\.tmp)
  11. # Describe the sink
  12. a3.sinks.k3.type = hdfs
  13. a3.sinks.k3.hdfs.path = hdfs://hadoop102:9000/flume/upload/%Y%m%d/%H
  14. #上传文件的前缀
  15. a3.sinks.k3.hdfs.filePrefix = upload-
  16. #是否按照时间滚动文件夹
  17. a3.sinks.k3.hdfs.round = true
  18. #多少时间单位创建一个新的文件夹
  19. a3.sinks.k3.hdfs.roundValue = 1
  20. #重新定义时间单位
  21. a3.sinks.k3.hdfs.roundUnit = hour
  22. #是否使用本地时间戳
  23. a3.sinks.k3.hdfs.useLocalTimeStamp = true
  24. #积攒多少个Event才flush到HDFS一次
  25. a3.sinks.k3.hdfs.batchSize = 100
  26. #设置文件类型,可支持压缩
  27. a3.sinks.k3.hdfs.fileType = DataStream
  28. #多久生成一个新的文件
  29. a3.sinks.k3.hdfs.rollInterval = 600
  30. #设置每个文件的滚动大小大概是128M
  31. a3.sinks.k3.hdfs.rollSize = 134217700
  32. #文件的滚动与Event数量无关
  33. a3.sinks.k3.hdfs.rollCount = 0
  34. #最小冗余数
  35. a3.sinks.k3.hdfs.minBlockReplicas = 1
  36. # Use a channel which buffers events in memory
  37. a3.channels.c3.type = memory
  38. a3.channels.c3.capacity = 1000
  39. a3.channels.c3.transactionCapacity = 100
  40. # Bind the source and sink to the channel
  41. a3.sources.r3.channels = c3
  42. a3.sinks.k3.channel = c3

启动监控文件夹命令

  1. bin/flume-ng agent --conf conf/ --name a3 --conf-file job/flume-dir-hdfs.conf

说明:
在使用spooldir(Spooling Directory Source)时

  1. 不要在监控目录中创建并持续修改文件
  2. 上传完成的文件会以.COMPLETED结尾
  3. 被监控文件夹每600毫秒扫描一次文件变动

向upload文件夹中添加文件

  1. touch file1.txt
  2. touch file2.tmp
  3. touch flie3.log

结果

\-w501

\-w1207

发表评论

表情:
评论列表 (有 0 条评论,428人围观)

还没有评论,来说两句吧...

相关阅读