自定义flume收集日志格式

日志文件格式(说明:两行日志是一条数据)
[2019-10-10 09:42:53]
hello world
[2019-10-10 09:43:53]
hello text
[2019-10-10 09:44:53]
hello hello
1.代码实现
package cn.tedu.source;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.EventDrivenSource;
import org.apache.flume.channel.ChannelProcessor;
import org.apache.flume.conf.Configurable;
import org.apache.flume.event.EventBuilder;
import org.apache.flume.source.AbstractSource;

public class AuthSource extends AbstractSource implements EventDrivenSource, Configurable {
private String path;
private ExecutorService es;
// 这个方法是用于获取格式文件中的属性值
@Override
public void configure(Context context) {
path = context.getString(“filePath”);
}
@Override
public synchronized void start() {
es = Executors.newFixedThreadPool(5);
// 获取Channel
ChannelProcessor cp = super.getChannelProcessor();
es.submit(new ReadLogThread(path, cp));
}
@Override
public synchronized void stop() {
es.shutdown();
}

}


class ReadLogThread implements Runnable {
private BufferedReader reader;
private ChannelProcessor cp;
public ReadLogThread(String path, ChannelProcessor cp) {
File file = new File(path);
if (file.isDirectory())
throw new IllegalArgumentException(path);
if (!path.endsWith(“log”))
throw new IllegalArgumentException(path);
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
this.cp = cp;
}

@Override
public void run() {

	while (true) {
		try {
			String line1 = reader.readLine();
			if (line1 == null)
				return;
			String line2 = reader.readLine();
			if (line2 == null)
				return;
			// [2019-10-09 07:25:24]
			// a.avi get 10.3.53.3
			// Flume中将每一条日志封装成一个Event对象
			Map<String, String> headers = new HashMap<>();
			String[] arr = line1.split(" ");
			headers.put("date", arr[0].substring(1));
			headers.put("time", arr[1].substring(0, arr[1].length() - 1));
			// 创建一个Event对象
			Event e = EventBuilder.withBody(line2.getBytes(), headers);
			cp.processEvent(e);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值