日志文件格式(说明:两行日志是一条数据)
[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();
}
}
}
}