BufferedReader br = new BufferedReader(
new FileReader("/path/to/log.txt"));
String line = null;
while (true ) {
line = br.readLine();
if (line == null) {
//wait till more data is appended to file
Thread.sleep(1000);
}
else {
//send data to Kafka
producer.send(new ProducerRecord<String, String>("my-topic", "my-key", line));
}
}
2条答案
按热度按时间h6my8fg21#
使用kafka日志附加器将日志重新定向到中央代理。
https://logging.apache.org/log4j/2.x/manual/appenders.html#kafkaappender
xtfmy6hx2#
该逻辑与Kafka无关,但您需要持续监听文件中的数据,如下所示: