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()); } }
配置文件的修改org.mybatis.spring.boot mybatis-spring-boot-starter2.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(){ //查询所有数据 Listlist = userMapper.findAll(); System.out.println(list); //利用bulk进行导入 } }
证明可以正确连接数据库,并获取数据
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 { //查询所有数据 Listlist = 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); } }
运行结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)