es批量分页导入数据
pojo类:
(将时间类的加上注解)
service层:
(注意的是查找器无条件,就是查找所有,发送请求是最后发送)
修改:2022.09.19 page是从页数1开始
@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
@Autowired
private RestHighLevelClient client;
@Autowired
private ItemMapper itemMapper;
@Override
public void prepareData() {
List<Item> items = itemMapper.selectList(null);
//获得总量
int size = items.size();
//计算页数
int page = size % 2000 == 0 ? size / 2000 : size / 2000 + 1;
int i = 0;
//查找数据
//LambdaQueryWrapper<Item> lqw = Wrappers.lambdaQuery(Item.class);
while (i <= page) {
Page<Item> itemPage = new Page<>(i, 2000);
Page<Item> page1 = page(itemPage);
List<Item> records = page1.getRecords();
//创建请求
BulkRequest request = new BulkRequest();
for (Item record : records) {
request.add(new IndexRequest("item")
.id(record.getId().toString())
.source(JSON.toJSONString(record), XContentType.JSON));
//发送请求
}
try {
client.bulk(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
i++;
}
}
controller层:
(该层只是调用,方法名自取)
@GetMapping("/prepareData")
public void prepareData() throws IOException {
itemService.prepareData();
//itemService.prepareData2();
}
标签:批量,prepareData,record,2000,导入,page,es,size
From: https://www.cnblogs.com/wzztg/p/16706219.html