flume监听文件追加内容并上传到HDFS

一、监听单个追加文件

1.1 需求

监控test.log日志文件,将日志文件追加的内容上传到hdfs

1.2 准备工作

Flume要想将数据输出到HDFS,必须持有Hadoop相关jar包,将对应jar复制到 /flume/lib 文件夹下
这里使用hadoop版本:hadoop-2.7.6

##需要的Jar
hadoop-common-2.7.6.jar
commons-configuration-1.6.jar
hadoop-auth-2.7.6.jar
hadoop-hdfs-2.7.6.jar
commons-io-2.4.jar
htrace-core-3.1.0-incubating.jar

##对应hadoop目录
/opt/apps/hadoop-2.7.6/share/hadoop/common
/opt/apps/hadoop-2.7.6/share/hadoop/common/lib
/opt/apps/hadoop-2.7.6/share/hadoop/common/lib
/opt/apps/hadoop-2.7.6/share/hadoop/hdfs
/opt/apps/hadoop-2.7.6/share/hadoop/hdfs/lib
/opt/apps/hadoop-2.7.6/share/hadoop/hdfs/lib

1.3 编写配置文件

在flume路径下的job路径中新建flume-hdfs.conf

vim /opt/apps/apache-flume-1.9.0-bin/job/flume-hdfs.conf

此文件编写内容如下

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

# Describe/configure the sources
# exec即execute执行命令
a1.sources.r1.type = exec
# 要执行的命令
a1.sources.r1.command = tail -F /data/test.log
# 执行shell脚本的绝对路径
a1.sources.r1.shell = /bin/bash -c

# Describe the sink
a1.sinks.k1.type = hdfs
# 上传到hdfs的路径
a1.sinks.k1.hdfs.path = hdfs://slave1:9000/flume/%Y%m%d%H
# 文件前缀
a1.sinks.k1.hdfs.filePrefix = logs-
# 是否按时间新建文件夹
a1.sinks.k1.hdfs.round = true
# 定义多久新建文件夹
a1.sinks.k1.hdfs.roundValue = 1
# 重新定义时间单位
a1.sinks.k1.hdfs.roundUnit = hour
# 是否使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
# flush到hdfs需要积攒event的数量
a1.sinks.k1.hdfs.batchSize = 50
# 设置文件类型
a1.sinks.k1.hdfs.fileType = DataStream
# 多久生成一个新的文件(单位:秒)
a1.sinks.k1.hdfs.rollInterval = 60
# 接收多少数据生成一个新文件(单位:字节)
a1.sinks.k1.hdfs.rollSize = 134217700
# 文件的滚动与event数量无关
a1.sinks.k1.hdfs.rollCount = 0
# 最小冗余数
a1.sinks.k1.hdfs.minBlockReplicas = 1

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
# 最大存储
a1.channels.c1.capacity = 1000
# 一个source或者一个sink,每个事务中最大的事件数
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

注意:
1.a1.sinks.k1.hdfs.path需要和hadoop的core-site.xml指定hdfs路径一样
2.tail -f 和tail -F 区别
tail -f
等同于–follow=descriptor,根据文件描述符进行追踪,当文件改名或被删除,追踪停止
tail -F
等同于–follow=name --retry,根据文件名进行追踪,并保持重试,即该文件被删除或改名后,如果再次创建相同的文件名,会继续追踪

1.4 运行flume

因为需要落地到hdfs,要先确保hadoop集群

在这里插入图片描述

在flume路径下

bin/flume-ng agent --conf conf/ --name a1 --conf-file job/flume-hdfs.conf   -Dflume.root.logger=INFO,console 
#写入数据到test.log
echo "123" >> test.log

然后查看hdfs目录
在这里插入图片描述

二 监听目录多个追加文件

2.1 需求

监听/data/flume目录下多个追加文件并上传到HDFS,同时实现断点续传功能。

Exec source适用于监控一个实时追加的文件,不能实现断点续传;Spooldir Source适合用于同步新文件,但不适合对实时追加日志的文件进行监听并同步;而Taildir Source适合用于监听多个实时追加的文件,并且能够实现断点续传。

2.2 编写配置文件

vim  flume-taildir-hdfs.conf

添加内容

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

# Describe/configure the source
a3.sources.r3.type = TAILDIR
a3.sources.r3.positionFile = /data/flume/tail_dir.json
a3.sources.r3.filegroups = f1 f2
a3.sources.r3.filegroups.f1 = /data/flume/.*file.*
a3.sources.r3.filegroups.f2 = /data/flume/.*log.*

# Describe the sink
a3.sinks.k3.type = hdfs
a3.sinks.k3.hdfs.path = hdfs://slave1:9000/flume/%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 = 50
#设置文件类型,可支持压缩
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.3 启动flume

bin/flume-ng agent --conf conf/ --name a3 --conf-file job/flume-taildir-hdfs.conf   -Dflume.root.logger=INFO,console 

在监控目录下创建文件并追加

cd   /data/flume/
touch file1.txt
touch file2.txt
touch file3.txt
echo 123 >>file1.txt
echo 4545 >>file2.txt

查看效果
在这里插入图片描述

##查看tail_dir.json
cat tail_dir.json
[{"inode":1452290,"pos":4,"file":"/data/flume/file1.txt"},{"inode":1452302,"pos":5,"file":"/data/flume/file2.txt"},{"inode":1452322,"pos":0,"file":"/data/flume/file3.txt"}]
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以通过Flume的Spooling Directory Source来实现这个功能。Spooling Directory Source会监控一个指定的目录,当有新的文件被写入时,就会将文件内容作为事件传递给Flume的Channel。然后再通过Flume的Interceptor将文件中的逗号修改为竖杠,并存储到HDFS中。 具体的配置如下: 1. 在Flume的配置文件中添加一个Spooling Directory Source: ``` # define the Spooling Directory Source agent.sources = spoolDir agent.sources.spoolDir.type = spooldir agent.sources.spoolDir.spoolDir = /path/to/spool/directory agent.sources.spoolDir.fileHeader = true agent.sources.spoolDir.fileSuffix = .COMPLETED ``` 2. 添加一个Interceptor来修改文件内容 ``` # define the Interceptor to modify the file content agent.sources.spoolDir.interceptors = interceptor1 agent.sources.spoolDir.interceptors.interceptor1.type = regex_replace agent.sources.spoolDir.interceptors.interceptor1.regex = , agent.sources.spoolDir.interceptors.interceptor1.replaceString = | ``` 3. 添加一个HDFS Sink ``` # define the HDFS Sink agent.sinks = hdfsSink agent.sinks.hdfsSink.type = hdfs agent.sinks.hdfsSink.hdfs.path = /path/to/hdfs/directory agent.sinks.hdfsSink.hdfs.fileType = DataStream agent.sinks.hdfsSink.hdfs.rollInterval = 0 ``` 最后将Spooling Directory Source和HDFS Sink连接起来: ``` # connect the Source and Sink agent.sources.spoolDir.channels = memoryChannel agent.sinks.hdfsSink.channel = memoryChannel ``` 这样配置后,Flume就会监听指定的目录,当有新文件写入时,将文件内容作为事件传递给内存Channel。Interceptor会修改文件内容,然后将修改后的事件存储到HDFS中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值