ElasticSearch(11)bulk批量 *** 作与导入数据

ElasticSearch(11)bulk批量 *** 作与导入数据,第1张

ElasticSearch(11)bulk批量 *** 作与导入数据 1 项目环境准备

springboot整合es:项目代码说明

2 bulk批量 *** 作

都是使用add()方法实现的

未执行之前:


package com.yy.esdemo;


import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
public class demo2 {
    @Autowired
    private RestHighLevelClient client;

    @Test
    public void testBulk() throws IOException {

        BulkRequest bulkRequest = new BulkRequest();
        
        //批量 *** 作
        //1.删除1号记录
        DeleteRequest deleteRequest = new DeleteRequest("person","1");
        bulkRequest.add(deleteRequest);
        //2.添加7号记录
        Map map = new HashMap();
        map.put("name","七号");
        IndexRequest indexRequest = new IndexRequest("person").id("7").source(map);
        bulkRequest.add(indexRequest);
        //3.修改2号记录,名称改为李四
        Map map1 = new HashMap();
        map1.put("name","李四");
        UpdateRequest updateRequest = new UpdateRequest("person","2").doc(map1);
        bulkRequest.add(updateRequest);


        BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulk.status());
    }
}




3 导入数据

3.1 修改项目文件 导入坐标
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.0
        
        
            mysql
            mysql-connector-java
        
配置文件的修改

application.yml

elasticsearch:
  host: 192.168.156.131
  port: 9200

# datasource
spring:
  datasource:
    url: jdbc:mysql:///xueqiutest?serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
# mybatis
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.yy.esdemo.domain
实体类
public class User {
    private Integer id;
    private String username;
    private String password;

}
mapper接口

UserMapper.xml





    

测试连接是否正确
package com.yy.esdemo;

import com.yy.esdemo.domain.User;
import com.yy.esdemo.mapper.UserMapper;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class demo3 {
    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private UserMapper userMapper;

    
    @Test
    public void importData(){
        //查询所有数据
        List list = userMapper.findAll();
        System.out.println(list);
        //利用bulk进行导入
    }
}


证明可以正确连接数据库,并获取数据

3.2 批量导入测试代码
package com.yy.esdemo;

import com.alibaba.fastjson.JSON;
import com.yy.esdemo.domain.User;
import com.yy.esdemo.mapper.UserMapper;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;

@SpringBootTest
public class demo3 {
    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private UserMapper userMapper;

    
    @Test
    public void importData() throws IOException {
        //查询所有数据
        List list = userMapper.findAll();
        //利用bulk进行导入
        BulkRequest bulkRequest = new BulkRequest();
        //循环goodsList,创建IndexRequest添加数据
        for (User user:list){
            IndexRequest indexRequest = new IndexRequest("user");
            indexRequest.id(user.getId()+"").source(JSON.toJSONString(user), XContentType.JSON);
            bulkRequest.add(indexRequest);
        }
        client.bulk(bulkRequest, RequestOptions.DEFAULT);

    }
}

运行结果:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5681144.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存