定义一个方法,把数据库中的数据导入到ES中[Java] 纯文本查看 复制代码 public void addES() throws IOException {
//假如有9万条
List<Sku> skuList = skuMapper.selectAll();
HttpHost httpHost = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder builder = RestClient.builder(httpHost);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
int total = skuList.size();
int i = (total % 2000 == 0 ? total / 2000 : total / 2000 + 1);
for (int j = 1; j <= i; j++) {
BulkRequest bulkRequest = new BulkRequest();
if (j == i) {
for (int k = (j - 1) * 2000; k < total; k++) {
Sku sku = skuList.get(k);
IndexRequest indexRequest = new IndexRequest("sku", "doc", sku.getId());
Map map = new HashMap();
map.put("name", sku.getName());
map.put("price", sku.getPrice());
map.put("image", sku.getImage());
map.put("createTime", sku.getCreateTime());
map.put("spuId", sku.getSpuId());
map.put("categoryName", sku.getCategoryName());
map.put("brandName", sku.getBrandName());
map.put("spec", sku.getSpec());
map.put("saleNum", sku.getSaleNum());
map.put("commentNum", sku.getCommentNum());
indexRequest.source(map);
bulkRequest.add(indexRequest);
}
BulkResponse response = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
int status = response.status().getStatus();
String s = response.buildFailureMessage();
System.out.println(status);
break;
}
for (int k = (j - 1) * 2000; k < j * 2000; k++) {
Sku sku = skuList.get(k);
IndexRequest indexRequest = new IndexRequest("sku", "doc", sku.getId());
Map map = new HashMap();
map.put("name", sku.getName());
map.put("price", sku.getPrice());
map.put("image", sku.getImage());
map.put("createTime", sku.getCreateTime());
map.put("spuId", sku.getSpuId());
map.put("categoryName", sku.getCategoryName());
map.put("brandName", sku.getBrandName());
map.put("spec", sku.getSpec());
map.put("saleNum", sku.getSaleNum());
map.put("commentNum", sku.getCommentNum());
indexRequest.source(map);
bulkRequest.add(indexRequest);
}
BulkResponse response = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
int status = response.status().getStatus();
String s = response.buildFailureMessage();
}
restHighLevelClient.close();
}
|