我有一个json文件列表,必须推送到弹性搜索。我循环遍历列表中的每个文件,并创建一个RestTemplate对象来推送数据。这是我的循环:
for(int i=1;i<=fileCount;i++){
String fileName="D:/poc/JSON1/file" +date+".csv"+ i + ".json";
BulkLoadingJsonFiles loadjson=new BulkLoadingJsonFiles();
loadjson.storeRecords(fileName);
}
每次执行此循环时,我都会执行以下操作:
public void storeRecords(String filename) throws FileNotFoundException, IOException, ParseException
{
String uri="http://localhost:9200/bd/type/_bulk";
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new FileReader(filename));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
RestTemplate rt=new RestTemplate();
rt.postForEntity(uri,everything , null);
}
这适用于列表中的第一个文件。稍后,对于第二个文件,它会抛出此错误:
17:16:04.418 [main] DEBUG org.springframework.web.client.RestTemplate - POST request for "http://localhost:9200/bd/type/_bulk" resulted in 500 (Internal Server Error); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:384)
at com.fissionlabs.datageneration.BulkLoadingJsonFiles.storeRecords(BulkLoadingJsonFiles.java:29)
at com.fissionlabs.practise.Practise.main(Practise.java:96)