Java连接ES, 将Mysql中数据同步到ES中:
主要用到了bulk.add()方法:
public void findByDistrictId() throws IOException {
// 数据库中查询数据
List<Air> airList = airMapper.findByDistrictId(null,null,null,null);
BulkRequest bulkRequest = new BulkRequest();
for (Air air : airList) {
/*将对象转换成字符串*/
String jsonString = JSON.toJSONString(air);
/*创建请求*/
IndexRequest indexRequest = new IndexRequest();
indexRequest.index("airinfo");
indexRequest.id(air.getId()+"");
indexRequest.source(jsonString, XContentType.JSON);
/*批量存入大量请求*/
bulkRequest.add(indexRequest);
}
/*请求转发,得到响应*/
BulkResponse resp =ElasticSearchUtilTE.getClient().bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(resp.status());
}