A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 老成 初级黑马   /  2019-8-14 16:07  /  909 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

定义一个方法,把数据库中的数据导入到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();
    }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马