pom依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>
java代码:
使用scroll游标查询遍历结果,导出到本地文件,scorll查询记得要设置游标过期时间,游标很占es资源,拿到结果后一定要清除scroll 的 id.
public static boolean getEs(String beginDate, String endDate) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.100.100", 9201, "http")));
//设定滚动时间间隔
final Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L));
SearchRequest searchRequest = new SearchRequest("index");//索引库
searchRequest.types("type");//索引表
searchRequest.scroll(scroll);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//设定每次返回多少条数据
searchSourceBuilder.size(pageNum);
//返回字段信息
searchSourceBuilder.fetchSource(new String[]{"id", "x_dip","i_dport"}, new String[]{});
//x_begintime 字段范围值过滤
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("x_begintime");
rangeQueryBuilder.gte(beginDate);//开始时间
rangeQueryBuilder.lte(endDate);//结束时间
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
boolBuilder.must(rangeQueryBuilder);
searchSourceBuilder.query(boolBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
String scrollId = searchResponse.getScrollId();
SearchHit[] searchHits = searchResponse.getHits().getHits();
//把导出的结果以JSON的格式写到文件里,filePath=写入文件路径
BufferedWriter out = new BufferedWriter(new FileWriter(filePath, true));
System.out.println("-----"+searchHits.length+"-----");
for (SearchHit searchHit : searchHits) {
String json = searchHit.getSourceAsString();
System.out.println(json);
out.write(json);
out.write("\r\n");
}
//遍历搜索命中的数据,直到没有数据
while (searchHits != null && searchHits.length > 0) {
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
scrollRequest.scroll(scroll);
searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
scrollId = searchResponse.getScrollId();
searchHits = searchResponse.getHits().getHits();
if (searchHits != null && searchHits.length > 0) {
System.out.println("-----"+searchHits.length+"-----");
for (SearchHit searchHit : searchHits) {
String json = searchHit.getSourceAsString();
System.out.println(json);
out.write(json);
out.write("\r\n");
}
}
}
//清除滚屏
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
//也可以选择setScrollIds()将多个scrollId一起使用
clearScrollRequest.addScrollId(scrollId);
ClearScrollResponse clearScrollResponse = null;
try {
clearScrollResponse = client.clearScroll(clearScrollRequest,RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
boolean succeeded = clearScrollResponse.isSucceeded();
if(succeeded){
System.out.println(filePath);
}
out.close();
client.close();
//成功返回true
return succeeded;
}