数据量小的方法一:
文章来源地址https://www.toymoban.com/news/detail-511702.html
首先必须创建client
public class ClientConfig {
/**
* 批量导入到es中必须的
* @return RestHighLevelClient
*/
@Bean
public RestHighLevelClient restHighLevelClient(){
return new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.150.102:9200")
));
}
}
批量导入数据量大时,用分页
public class FeignTest {
@Autowired
private ItemMapperService itemService;
@Autowired
private RestHighLevelClient client;
/**
* 批量导入文档 方式一
*/
@Test
void testBulkRequest() throws IOException {
buildPage(0, 20000);
}
private void buildPage(int current, int size) throws IOException {
//构建分页器
List<Item> records = itemService.page(new Page<>(current, size)).getRecords();
//判断
if (records == null||records.size() == 0) {
return;
}
//转换为文档类型
// 1.创建Request
BulkRequest request = new BulkRequest();
// 2.准备参数,添加多个新增的Request
for (Item list : records) {
// 2.1.转换为文档类型HotelDoc
ItemDoc itemDoc= new ItemDoc(list);
//创建新增文档的request对象
request.add(new IndexRequest("item")
.id(itemDoc.getId().toString())
.source(JSON.toJSONString(itemDoc), XContentType.JSON));
}
// 3.发送请求
client.bulk(request, RequestOptions.DEFAULT);
buildPage(current + 1, size);
}
}
Json的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
文章来源:https://www.toymoban.com/news/detail-511702.html
到了这里,关于将数据库的数据导入es中的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!