如何从elasticsearch索引中获取所有文档并将其转换为csv java api

mzillmmw  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(0)|浏览(266)

在ElasticSearch索引7.9版中,我有超过25000个数据的巨大记录。我得到了所有的文件,并将其转换为csv。我尝试使用滚动api,但我没有得到预期的结果。我在下面给出了我的代码。
主要方法

FileWriter writer = new FileWriter("D:\\Users\\jjj\\jjjjj\\SQLEXPRADV_x64_ENU\\sto1.csv");
RestHighLevelClient esClient = esClient();
SearchRequest searchRequest = new SearchRequest("test-2020.10.09");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchSourceBuilder.size(1000);
searchRequest.source(searchSourceBuilder);
searchRequest.scroll(TimeValue.timeValueSeconds(200));
SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
String scrollId = searchResponse.getScrollId();
SearchHits hits = searchResponse.getHits();
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
scrollRequest.scroll(TimeValue.timeValueSeconds(2));
SearchResponse searchScrollResponse = esClient.scroll(scrollRequest, RequestOptions.DEFAULT);
scrollId = searchScrollResponse.getScrollId();
hits = searchScrollResponse.getHits();
SearchResponse searchRespon = esClient.scroll(scrollRequest, RequestOptions.DEFAULT);
final SearchHit[] searchHits = searchRespon.getHits().getHits();

for (SearchHit hit : searchHits) {
    courseList.add(hit.getSourceAsString());
}

System.out.println(hits.getTotalHits().value);
System.out.println(hits.getHits().length);
System.out.println(scrollId);
System.out.println(courseList.size());

String collect = courseList.stream().collect(Collectors.joining(","));
System.out.println(collect);

writer.write(collect);
writer.close();

如何使用 RestHighLevelClient java API?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题