1.写一个测试类
readFile
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class LogsRead {
@Autowired
KafkaTemplate kafkaTemplate;
@Test
public void loadFile() throws InterruptedException, IOException {
String path="d:/logs/";
readFile(new File(path));
Thread.sleep(100000);
System.out.println("读取结果");
}
private void readFile(File file) throws IOException {
//获取所有文件
File[] files = file.listFiles();
if (files!=null&&files.length>0){
for (File file1 : files) {
if (file1.isFile()){
//读取文件
System.out.println("读取文件的名字是:"+file1.getName());
BufferedReader reader = new BufferedReader(new FileReader(file1));
String str="";
while ((str=reader.readLine())!=null){
//发送给kafka
kafkaTemplate.send("test","import",str);
}
}else {
this.readFile(file1);
}
}
}
}
}
2.将文件写入es
写一个loginfo的实体类

写一个接口

将消息接收 写入到es

该博客展示了如何在Java中利用Spring框架和KafkaTemplate读取文件内容,通过递归遍历目录读取所有文件,并将内容发送到Kafka主题。之后,这些消息被接收并写入Elasticsearch(ES)进行存储。Loginfo实体类和接口的提及表明存在数据持久化的操作。
821

被折叠的 条评论
为什么被折叠?



