elasticSearch基于restAPI批量提交数据
public void openfile() {
String filepath="D:\\DATA\\city_id=341800\\001033_0";
String encoding ="UTF-8";
RestHighLevelClient restClient=new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.30.128",9200,"http")));
File files =new File(filepath);
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(files),encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
try {
int i=1;
BulkRequest request = new BulkRequest();
BulkResponse bulkResponse;
while((lineTxt = bufferedReader.readLine()) != null){
String[] strs=lineTxt.split(",");
//System.out.println(strs.length);
request.add(new IndexRequest("test-index", "test", ""+i+"").source(XContentType.JSON,"1", strs[0],
"2",checkNull(strs[1]),
"3",checkNull(strs[3]),
"4",checkNull(strs[4]),
"5",checkNull(strs[5]),
....
"67",checkNull(strs[58])));
if(i%1000==0) {
System.out.println(i);
restClient.bulk(request);
}
i++;
}
restClient.bulk(request);
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException | FileNotFoundException e1) {
e1.printStackTrace();
}
}