spring data elasticsearch 大数据量导入elasticsearch
经过线上测试,适用于百万数据量的导入,只需要五分钟
@Autowired
private ElasticsearchTemplate esTemplate;
@Autowired
private LemBookRepository lemBookRepository;
@Test
public void setData() {
//获得所有book
Iterable<LemBook> list = lemBookRepository.getAll();
Integer count=0;
List<IndexQuery> queryList = new ArrayList<>();
for (LemBook lemBook : list) {
ESLemBook esLemBook = new ESLemBook();
BeanUtils.copyProperties(lemBook, esLemBook);
IndexQuery indexQuery = new IndexQuery();
indexQuery.setId(esLemBook.getId().toString());
indexQuery.setObject(esLemBook);
indexQuery.setIndexName("book");
indexQuery.setType("book");
queryList.add(indexQuery);
if(count%1000==0){
esTemplate.bulkIndex(queryList);
queryList.clear();
}
count++;
}
if(queryList.size()>0){
esTemplate.bulkIndex(queryList);
}
}