Flume-实时监控多个文件的追加内容(TailDir Source)

实时监控目录下的多个追加文件(断点续传)

exec-source适用于监控一个实时追加的文件,但不能保证数据不丢失;spooldir-source能够保证数据不丢失,且能够实现断点续传,但延迟略高,不能实时监控;而taildir-source即能够实现断电续传,又可以保证数据不丢失,还能够实时监控,并可以同时监控多个文件夹中的多个文件。

分析:使用Flume监控整个目录的实时追加文件,并打印到控制台输出

在这里插入图片描述

先创建好需要监控的文件。

配置文件如下:

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

# Describe/configure the source
a1.sources.r1.type = TAILDIR
# 监控的文件夹可以配置多个,文件也可以配置多个
# 也可以使用正则表达式(注意不会匹配文件系统的目录)只是用来匹配文件名
# 这里如果都写f1的话下面的会覆盖上面的
a1.sources.r1.filegroups = f1 f2
a1.sources.r1.filegroups.f1 = /home/bd/tmp/tailDirTest/log1
a1.sources.r1.filegroups.f2 = /home/bd/tmp/tailDirTest/log2
# 正则表达式方式如下
# a1.sources.r1.filegroups = f1
# a1.sources.r1.filegroups.f1 = /home/bd/tmp/tailDirTest/log.*
# 记录已完成的文件的位置信息,是一个json文件
a1.sources.r1.positionFile = /home/bd/tmp/tailDirPosition/position.json

# Describe the sink
a1.sinks.k1.type = logger

# 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

运行flume,初始情况下两个文件中均没有数据,我们查看position内容如下

[{"inode":115,"pos":0,"file":"/home/bd/tmp/tailDirTest/log1"},{"inode":119,"pos":0,"file":"/home/bd/tmp/tailDirTest/log2"}]

为log1加入hello,为log2加入hello

在这里插入图片描述

此时position中的内容如下:

[{"inode":115,"pos":6,"file":"/home/bd/tmp/tailDirTest/log1"},{"inode":119,"pos":6,"file":"/home/bd/tmp/tailDirTest/log2"}]

然后将flume关闭,关闭后向log1中加入word,log2中加入hello和word,再开启flume,结果如下:

在这里插入图片描述

[{"inode":115,"pos":11,"file":"/home/bd/tmp/tailDirTest/log1"},{"inode":119,"pos":17,"file":"/home/bd/tmp/tailDirTest/log2"}]

可以看到flume重新连接之后,实现了断点续传。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值