实现flume tailfsource解决丢数等问题

本文介绍了在使用Flume实时收集日志数据时,由于tail -F方法存在的丢数问题,转而采用自定义的TAILFSource进行优化。通过配置TAILFSource,如设置readingfilepath、historyfilepath、offsetrecordfile和batchSize等参数,以避免数据丢失并提高性能。同时提到了BufferedRandomAccessFile作为RandomAccessFile的优化方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用flume过程中,实时的收集数据时,一般使用exec的source,通过tail -F的形式收集日志内容,但是tail -F存在包括丢数在内一系列问题,实现tailfsource解决:


public class TailingFileSource extends AbstractSource implements
EventDrivenSource, Configurable {


private static final Logger logger = LoggerFactory
.getLogger(TailingFileSource.class);


private SourceCounter sourceCounter;
private long restartThrottle;
private boolean restart;
private Integer bufferCount;
private long batchTimeout;
private ExecutorService executor;
private Future<?> runnerFuture;
private String readingFilePath;
private String historyFilePath;
private String offsetRecordFile;
private ChannelProcessor channelProcessor;
private ReadingFile reader;


@Override
public void configure(Context context) {
// TODO Auto-generated method stub


restartThrottle = context
.getLong(
TailingFileSourceConfigurationConstants.CONFIG_RESTART_THROTTLE,
TailingFileSourceConfigurationConstants.DEFAULT_RESTART_THROTTLE);


restart = context.getBoolean(
TailingFileSourceConfigurationConstants.CONFIG_RESTART,
TailingFileSourceConfigurationConstants.DEFAULT_RESTART);


bufferCount = context.getInteger(
TailingFileSourceConfigurationConstants.CONFIG_BATCH_SIZE,
TailingFileSourceConfigurationConstants.DEFAULT_BATCH_SIZE);


batchTimeout = context.getLong(
TailingFileSourceConfigurationConstants.CONFIG_BATCH_TIME_OUT,
TailingFileSourceConfigurationConstants.DEFAULT_BATCH_TIME_OUT);


readingFilePath = context
.getString(TailingFileSourceConfigurationConstants.CONFIG_READING_FILE_PATH);


Preconditions.checkState(readingFilePath != null,
"The parameter readingFilePath must be specified");


historyFilePath = context
.getString(TailingFileSourceConfigurationConstants.CONFIG_HISTORY_FILE_PATH);


Preconditions.checkState(historyFilePath != null,
"The parameter historyFilePath must be specified");


offsetRecordFile = context
.getString(TailingFileSourceConfigurationConstants.CONFIG_OFFSET_RECORD_FILE);


Preconditions.checkState(offsetRecordFile != null,
"The parameter offsetRecordFile must be specified");


if (sourceCounter == null) {
sourceCounter = new SourceCounter(getName());
}
}


private static class ReadingFile implements Runnable {


public ReadingFile(String readingFilePath, int bufferCount,
long batchTimeout, SourceCounter sourceCounter,
String historyFilePath, String offsetRecordFile,
ChannelProcessor channelProcessor, boolean restart,
long restartThrottle) {
this.re
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值