zoukankan      html  css  js  c++  java
  • springboot使用RestHighLevelClient批量插入

    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),不然只会插入最后一条记录(这样插入不会覆盖已经存在的Id,也就是不能更新)
           //request.add(new IndexRequest(index,type).opType("create").id(map.remove("id").toString()).source(map));
            
    request.add(new IndexRequest(index, type, String.valueOf(map.remove("id"))).source(map,XContentType.JSON));
    
        
          restHighLevelClient.bulk(request); 
    
      } 
    }

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

  • 相关阅读:
    接口测试之Postman简介
    postman发送get请求
    postman添加权限验证
    接口测试基础
    postman发送post请求
    postman测试上传文件
    1 R语言介绍
    《荣枯鉴》明鉴卷六
    《荣枯鉴》节仪卷五
    《荣枯鉴》交结卷四
  • 原文地址:https://www.cnblogs.com/cq-yangzhou/p/10762180.html
Copyright © 2011-2022 走看看