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

1 项目环境准备

springboot整合es:项目代码说明

2 bulk批量操作

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

未执行之前:
ElasticSearch(11)bulk批量操作与导入数据_第1张图片
ElasticSearch(11)bulk批量操作与导入数据_第2张图片
ElasticSearch(11)bulk批量操作与导入数据_第3张图片

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号记录
            #  2.添加7号记录
            #  3.修改2号记录,名称改为李四
         */
        //批量操作
        //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());
    }
}

ElasticSearch(11)bulk批量操作与导入数据_第4张图片
ElasticSearch(11)bulk批量操作与导入数据_第5张图片
ElasticSearch(11)bulk批量操作与导入数据_第6张图片
ElasticSearch(11)bulk批量操作与导入数据_第7张图片

3 导入数据

ElasticSearch(11)bulk批量操作与导入数据_第8张图片

3.1 修改项目文件

导入坐标


        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.0version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>

配置文件的修改

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


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yy.esdemo.mapper.UserMapper">
    <select id="findAll" resultType="User">
        select * from t_user
    select>
mapper>

测试连接是否正确

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<User> list = userMapper.findAll();
        System.out.println(list);
        //利用bulk进行导入
    }
}

ElasticSearch(11)bulk批量操作与导入数据_第9张图片
证明可以正确连接数据库,并获取数据

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<User> 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);

    }
}

运行结果:
ElasticSearch(11)bulk批量操作与导入数据_第10张图片
ElasticSearch(11)bulk批量操作与导入数据_第11张图片

你可能感兴趣的:(#,ELK,elasticsearch,搜索引擎,大数据)