首页 > 编程语言 > 详细

springboot使用RestHighLevelClient批量插入

时间:2019-04-24 15:01:57      阅读:1310      评论:0      收藏:0      [点我收藏+]

1、引入maven(注意版本要一致

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.4.3</version>
        </dependency>   

2、配置.properties

spring.elasticsearch.rest.uris=http://127.0.0.1:9200
spring.elasticsearch.rest.password=
spring.elasticsearch.rest.username=

3、使用

 @Autowired
    private RestHighLevelClient restHighLevelClient;

 private void bulkPutIndex(List<Map<String, Object>> list ) throws IOException {
        if (JudgeUtil.isEmpty(list)){
            return;
        }
        String index = "test";
        String type = "test";
        int size = list.size();
        BulkRequest request = new BulkRequest();
        for (int i = 0; i < size; i++) {
            Map<String, Object> map = list.get(i);
       //这里必须每次都使用new IndexRequest(index,type),不然只会插入最后一条记录
    request.add(new IndexRequest(index,type).opType("create").id(map.remove("id").toString()).source(map)); 
    
      restHighLevelClient.bulk(request); 

  } 
}

 参考地址:https://www.cnblogs.com/leeSmall/p/9218779.html

springboot使用RestHighLevelClient批量插入

原文:https://www.cnblogs.com/cq-yangzhou/p/10762180.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!