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