Flume 应用2(单目录多个文件(Spooling Directory source、memory channel、hdfs sink ))

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.先上传文件后改名
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Flume采集HDFS上的文件,你需要使用Flume的Spooling Directory Source。这个source会监视一个指定目录中的文件,当有新的文件到达时,会将文件内容读取到Flume的Event中,然后将这些Event发送到指定的Channel中。 以下是一个简Flume配置文件示例,用于采集HDFS上的文件: ``` # 定义一个agent agent.sources = spoolDir agent.channels = memoryChannel agent.sinks = hdfsSink # 配置spoolDir source agent.sources.spoolDir.type = spooldir agent.sources.spoolDir.spoolDir = /path/to/hdfs/directory agent.sources.spoolDir.fileHeader = true agent.sources.spoolDir.basenameHeader = true agent.sources.spoolDir.batchSize = 1000 agent.sources.spoolDir.batchTimeout = 1000 # 配置memoryChannel channel agent.channels.memoryChannel.type = memory agent.channels.memoryChannel.capacity = 10000 agent.channels.memoryChannel.transactionCapacity = 1000 # 配置hdfsSink sink agent.sinks.hdfsSink.type = hdfs agent.sinks.hdfsSink.hdfs.path = hdfs://namenode:8020/path/to/hdfs/directory agent.sinks.hdfsSink.hdfs.filePrefix = %{basename} agent.sinks.hdfsSink.hdfs.useLocalTimeStamp = true agent.sinks.hdfsSink.hdfs.fileType = DataStream agent.sinks.hdfsSink.hdfs.writeFormat = Text agent.sinks.hdfsSink.hdfs.rollInterval = 3600 agent.sinks.hdfsSink.hdfs.rollSize = 0 agent.sinks.hdfsSink.hdfs.rollCount = 0 # 配置sourcesink之间的channel agent.sources.spoolDir.channels = memoryChannel agent.sinks.hdfsSink.channel = memoryChannel ``` 在这个示例中,我们使用Spooling Directory Source来监视HDFS上的一个目录。当有新的文件到达时,Flume会将文件内容读取到Event中,并将这些Event发送到Memory Channel中。然后,HDFS Sink会从Memory Channel中读取Event,并将其写入HDFS文件中。 注意,这个示例中的配置文件只是一个简的示例。你需要根据实际情况对其进行修改,以适应你的具体需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值