分享

Flume TaildirSource如何实现递归动态目录

qcbb001 2017-3-24 14:33:59 发表于 其它 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 2 18400



需求描述
使用了SpoolDirectorySource可以配置一个监听目录,会监听该目录下所有的文件,但是如果配置目录下面嵌套了子目录,则无法监听,通过修改源码,我们可以递归地对配置目录的所有子目录的所有文件进行监听,见上一篇文章 Flume的Spooling Directory Source支持Sub-directories
但是,SpoolDirectorySource的一个限制还没有消除,即监听目录下的文件不允许动态变化。比如我在收集yarn的nodemanager的日志时候,发现监听目录下面的文件是动态追加的,而不是先生成一个.tmp临时文件,全部写完后rename,这样,我们还需要对SpoolDirectorySource进行改进:可以对动态追加内容的文件进行实时监听。
这有点像:ExecSource + SpoolDirectorySource 的一个source

解决方案
先看看能否踩在巨人的肩膀上,查看flume1.7.0的 realease note ,发现增加了一个Taildir Source,点进去发现,这不就是我们需要的吗!
我贴一下官方的description:

Description
This is the proposal of implementing a new tailing source.
This source watches the specified files, and tails them in nearly real-time once appends are detected to these files.
This source is reliable and will not miss data even when the tailing files rotate.
It periodically writes the last read position of each file in a position file using the JSON format.
If Flume is stopped or down for some reason, it can restart tailing from the position written on the existing position file.
It can add event headers to each tailing file group.
A attached patch includes a config documentation of this.
This source requires Unix-style file system and Java 1.7 or later.

好,有了它,下面就有两种解决方案:
1. 直接使用flume1.7.0
2. 将该feature增加到flume1.6.0中
由于我们已经对flume1.6.0做了比较大的改动,所以显然需要第二种方案。
集成过程中主要改动的是两个module
1. 将flume-taildir-source添加到flume-ng-sources
2. 编译根据提示一步一步修改flume-ng-core下的部分文件

增加递归监听子目录的特性
贴一下TaildirSource的配置:

[mw_shl_code=bash,true]# source2: tail_dir_source -----------------
    agent.sources.tail_dir_source.type = org.apache.flume.source.taildir.TaildirSource
    agent.sources.tail_dir_source.channels = memory_channel
    agent.sources.tail_dir_source.positionFile = /tmp/taildir_position.json
    agent.sources.tail_dir_source.filegroups = f1
    agent.sources.tail_dir_source.filegroups.f1 = /home/urey/yarn_log/.*
    agent.sources.tail_dir_source.batchSize = 100
    agent.sources.tail_dir_source.backoffSleepIncrement  = 1000
    agent.sources.tail_dir_source.maxBackoffSleep  = 5000
    agent.sources.tail_dir_source.recursiveDirectorySearch = true
    agent.sources.tail_dir_source.yarnApplicationHeader = true
    agent.sources.tail_dir_source.yarnContainerHeader = true[/mw_shl_code]
类似SpoolDirectorySource增加该feature所做的工作,我们最后还需要给TaildirSource增加该feature以满足我们的需求。

改动类似《Flume的Spooling Directory Source支持Sub-directories》所涉及到,需要注意的是,
filegroups可以指定多个监听源,每个源指定相应的监听路径下面的哪些文件,这里的下面经过改动后会递归地包含下面的所有子目录。

比如:
配置了/home/urey/yarn_log/.*,会监听:

[mw_shl_code=bash,true]/home/urey/yarn_log/test1.log
/home/urey/yarn_log/dir1/test2.log
/home/urey/yarn_log/dir1/dir2/test3.log
...
等等[/mw_shl_code]

最新的代码我已上传到github上,欢迎试用:
注:
由于flume1.7.0本身还没有发布,所以TaildirSource可能存在缺陷,如果遇到问题,需要通过查看日志定位问题,看社区是否有相应的issue和解决方案。



来自:csdn
作者:qwurey

已有(2)人评论

跳转到指定楼层
rebo 发表于 2018-4-10 20:47:16
为什么少了TaildirMatcher.java文件
回复

使用道具 举报

醉半城 发表于 2017-3-27 13:44:46
1.7不是去年早发布了么
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条