Flume案例四:实时监控目录下多个新文件(Spooling Directory Source)

本文接上篇博客:Flume介绍、安装、使用案例、自定义Source/Sink、监控
Flume 版本:1.9.0
本文hdfs sink,需 Hadoop 支持,Hadoop相关内容,请参考:Hadoop专栏

1.实时监控目录下多个新文件

选型:spooling directory source + memory channel + hdfs sink

文档参考:
spooling directory sourcehttp://flume.apache.org/releases/content/1.9.0/FlumeUserGuide.html#spooling-directory-source
memory channelhttp://flume.apache.org/releases/content/1.9.0/FlumeUserGuide.html#memory-channel
hdfs sinkhttp://flume.apache.org/releases/content/1.9.0/FlumeUserGuide.html#hdfs-sink

提示:
  Exec source 适用于监控一个实时追加的文件,但不能保证数据不丢失Spooling Directory Source 能够保证数据不丢失,且能够实现断点续传,但延迟较高,不能实时监控而 Taildir Source 既能够实现断点续传,又可以保证数据不丢失,还能够进行实时监控,集两者优点于一身,更推荐使用Taildir Source。

推荐:
  如果你要使用 Spooling Directory Source,生产上更推荐使用 Taildir source,本文仅做了解学习使用。Taildir Source 参考:https://blog.csdn.net/lzb348110175/article/details/118189312

2.需求图示

在这里插入图片描述

3.flume配置

flume-spooldir-hdfs.conf

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

# Describe/configure the source
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /opt/module/testdir
a1.sources.r1.fileHeader = true
a1.sources.r1.fileSuffix = .COMPLETED
#忽略所有以.tmp 结尾的文件,不上传
a1.sources.r1.ignorePattern = ([^ ]*\.tmp)

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume/spooldir/%Y-%m-%d/%H
a1.sinks.k1.hdfs.filePrefix = testlog
# 是否使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
# 是否按照时间滚动文件夹
a1.sinks.k1.hdfs.round = true
# 多少时间单位创建一个新的文件夹
a1.sinks.k1.hdfs.roundValue = 1
# 重新定义时间单位
a1.sinks.k1.hdfs.roundUnit = hour
# 积攒多少个 Event 才 flush 到 HDFS 一次
a2.sinks.k2.hdfs.batchSize = 1000
# 多久生成一个新的文件(seconds)
a1.sinks.k1.hdfs.rollInterval = 30
# 设置每个文件的滚动大小
a1.sinks.k1.hdfs.rollSize = 134217700
# 文件的滚动与 Event 数量无关
a1.sinks.k1.hdfs.rollCount = 0
# 设置文件类型,可支持压缩(不加该配置的话,Flume写入HDFS的文件会出现SEQ !org.apache.hadoop.io.LongWritable"org.apache.hadoop.io.BytesWritable)
a1.sinks.k1.hdfs.fileType = DataStream
# 关闭HDFS文件出错时重试次数,之前设为0即无限重试。现在改为2
test.sinks.sink.hdfs.closeTries=2

# 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

4.启动命令

bin/flume-ng agent -c conf -n a1 -f job/flume-spooldir-hdfs.conf

5.异常处理

写入HDFS,报如下错误:java.lang.NoSuchMethodError:com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V

  这是因为hadoop目录下得guava版本和flume下的guava版本的问题。进入 flume/lib 目录下,将 guava-11.0.2.jar 包移除即可

6.测试图示

  Spooling Directory Source 实时监听 testdir 目录,提前准备好两个测试文件test001.txttest002.txt,测试 mv 移动文件到 testdir 目录下,flume 端能够正常接收到新增的文件,通过 hdfs sink方式,将文件内容写出到 /flume/spooldir 目录下,测试数据如图所示:
在这里插入图片描述

测试结果,如图所示:
在这里插入图片描述


博主写作不易,加个关注呗

求关注、求点赞,加个关注不迷路 ヾ(◍°∇°◍)ノ゙

我不能保证所写的内容都正确,但是可以保证不复制、不粘贴。保证每一句话、每一行代码都是亲手敲过的,错误也请指出,望轻喷 Thanks♪(・ω・)ノ

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Flume的Spooling Directory Source是用于监控指定目录并将文件内容作为事件发送到Flume流程中的组件。要实现抓取文件后自动停止,可以使用Flume的File Channel组件和Flume的Exec Source组件。 具体实现步骤如下: 1. 配置Spooling Directory Source,将文件内容发送到File Channel中。 ```properties #定义agent agent.sources = spoolDirSource agent.channels = fileChannel agent.sinks = nullSink #定义source agent.sources.spoolDirSource.type = spooldir agent.sources.spoolDirSource.spoolDir = /data/spool agent.sources.spoolDirSource.fileHeader = true agent.sources.spoolDirSource.basenameHeader = true agent.sources.spoolDirSource.batchSize = 1000 agent.sources.spoolDirSource.pollDelay = 10000 agent.sources.spoolDirSource.channels = fileChannel #定义channel agent.channels.fileChannel.type = file agent.channels.fileChannel.checkpointDir = /data/flume/checkpoint agent.channels.fileChannel.dataDirs = /data/flume/data agent.channels.fileChannel.capacity = 10000000 agent.channels.fileChannel.transactionCapacity = 1000 #定义sink agent.sinks.nullSink.type = null agent.sinks.nullSink.channel = fileChannel ``` 2. 配置Exec Source,使用Exec Source来实现自动停止。 ```properties #定义agent agent.sources = execSource agent.channels = fileChannel agent.sinks = nullSink #定义source agent.sources.execSource.type = exec agent.sources.execSource.command = sh /data/stop_flume.sh agent.sources.execSource.batchSize = 1 agent.sources.execSource.channels = fileChannel #定义channel agent.channels.fileChannel.type = file agent.channels.fileChannel.checkpointDir = /data/flume/checkpoint agent.channels.fileChannel.dataDirs = /data/flume/data agent.channels.fileChannel.capacity = 10000000 agent.channels.fileChannel.transactionCapacity = 1000 #定义sink agent.sinks.nullSink.type = null agent.sinks.nullSink.channel = fileChannel ``` 3. 编写停止脚本stop_flume.sh,当该脚本被执行时,Exec Source会接收到一个事件并停止Flume。 ```shell #!/bin/bash touch /data/stop_flume ``` 4. 在Flume的启动脚本中,添加一个循环来监控停止标志文件的变化情况。当停止标志文件被创建时,停止Flume。 ```shell #!/bin/bash bin/flume-ng agent -n agent -c conf -f conf/flume-conf.properties & PID=$! while [ ! -f /data/stop_flume ] do sleep 1 done kill $PID ``` 这样,当停止标志文件被创建时,Exec Source会接收到一个事件并停止Flume。同时,File Channel中的所有事件都会被处理完毕。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扛麻袋的少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值