博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Elasticsearch Sliced Scroll分页检索案例分享
阅读量:6869 次
发布时间:2019-06-26

本文共 5422 字,大约阅读时间需要 18 分钟。

  hot3.png

The best elasticsearch highlevel java rest api----- 

Elasticsearch Sliced Scroll分页检索案例分享 

我们在文章《》中介绍了elasticsearch scroll的基本用法,本文介绍Elasticsearch Sliced Scroll分页检索功能。

1.准备工作

参考文档《》导入和配置es客户端

2.定义Sliced Scroll检索dsl

创建配置文件-在resources目录下定义文件scroll.xml

esmapper/scroll.xml

文件内容包含Sliced Scroll检索dsl语句-scrollSliceQuery

3.串行方式执行slice检索

/** * 串行方式执行slice scroll操作 */@Testpublic void testSliceScroll() {	ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/scroll.xml");	List
scrollIds = new ArrayList<>(); long starttime = System.currentTimeMillis(); //scroll slice分页检索 int max = 6; long realTotalSize = 0; for (int i = 0; i < max; i++) { Map params = new HashMap(); params.put("id", i); params.put("max", max);//最多6个slice,不能大于share数 params.put("size", 100);//每页100条记录 ESDatas
sliceResponse = clientUtil.searchList("agentstat-*/_search?scroll=1m", "scrollSliceQuery", params,Map.class); List sliceDatas = sliceResponse.getDatas(); realTotalSize = realTotalSize + sliceDatas.size(); long totalSize = sliceResponse.getTotalSize(); String scrollId = sliceResponse.getScrollId(); if (scrollId != null) scrollIds.add(scrollId); System.out.println("totalSize:" + totalSize); System.out.println("scrollId:" + scrollId); if (sliceDatas != null && sliceDatas.size() >= 100) {//每页100条记录,迭代scrollid,遍历scroll分页结果 do { sliceResponse = clientUtil.searchScroll("1m", scrollId, Map.class); String sliceScrollId = sliceResponse.getScrollId(); if (sliceScrollId != null) scrollIds.add(sliceScrollId); sliceDatas = sliceResponse.getDatas(); if (sliceDatas == null || sliceDatas.size() < 100) { break; } realTotalSize = realTotalSize + sliceDatas.size(); } while (true); } } //打印处理耗时和实际检索到的数据 long endtime = System.currentTimeMillis(); System.out.println("耗时:"+(endtime - starttime)+",realTotalSize:"+realTotalSize); //查询存在es服务器上的scroll上下文信息 String scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); System.out.println(scrolls); //处理完毕后清除scroll上下文信息 if(scrollIds.size() > 0) { scrolls = clientUtil.deleteScrolls(scrollIds); System.out.println(scrolls); } //清理完毕后查看scroll上下文信息 scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET); System.out.println(scrolls);}

4.并行方式执行slice检索

//用来存放实际slice检索总记录数long realTotalSize ;//辅助方法,用来累计每次scroll获取到的记录数synchronized void incrementSize(int size){	this.realTotalSize = this.realTotalSize + size;}/** * 并行方式执行slice scroll操作 */@Testpublic void testParralSliceScroll() {	final ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/scroll.xml");	final List
scrollIds = new ArrayList<>(); long starttime = System.currentTimeMillis(); //scroll slice分页检索 final int max = 6; final CountDownLatch countDownLatch = new CountDownLatch(max);//线程任务完成计数器,每个线程对应一个sclice,每运行完一个slice任务,countDownLatch计数减去1 for (int j = 0; j < max; j++) {//启动max个线程,并行处理每个slice任务 final int i = j; Thread sliceThread = new Thread(new Runnable() {//多线程并行执行scroll操作做,每个线程对应一个sclice @Override public void run() { Map params = new HashMap(); params.put("id", i); params.put("max", max);//最多6个slice,不能大于share数 params.put("size", 100);//每页100条记录 ESDatas
sliceResponse = clientUtil.searchList("agentstat-*/_search?scroll=1m", "scrollSliceQuery", params,Map.class); List sliceDatas = sliceResponse.getDatas(); incrementSize( sliceDatas.size());//统计实际处理的文档数量 long totalSize = sliceResponse.getTotalSize(); String scrollId = sliceResponse.getScrollId(); if (scrollId != null) scrollIds.add(scrollId); System.out.println("totalSize:" + totalSize); System.out.println("scrollId:" + scrollId); if (sliceDatas != null && sliceDatas.size() >= 100) {//每页100条记录,迭代scrollid,遍历scroll分页结果 do { sliceResponse = clientUtil.searchScroll("1m", scrollId, Map.class); String sliceScrollId = sliceResponse.getScrollId(); if (sliceScrollId != null) scrollIds.add(sliceScrollId); sliceDatas = sliceResponse.getDatas(); if (sliceDatas == null || sliceDatas.size() < 100) { break; } incrementSize( sliceDatas.size());//统计实际处理的文档数量 } while (true); } countDownLatch.countDown();//slice检索完毕后计数器减1 } }); sliceThread.start();//启动线程 } try { countDownLatch.await();//等待所有的线程执行完毕,计数器变成0 } catch (InterruptedException e) { e.printStackTrace(); } //打印处理耗时和实际检索到的数据 long endtime = System.currentTimeMillis(); System.out.println("耗时:"+(endtime - starttime)+",realTotalSize:"+realTotalSize); //查询存在es服务器上的scroll上下文信息 String scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET);// System.out.println(scrolls); //处理完毕后清除scroll上下文信息 if(scrollIds.size() > 0) { scrolls = clientUtil.deleteScrolls(scrollIds);// System.out.println(scrolls); } //清理完毕后查看scroll上下文信息 scrolls = clientUtil.executeHttp("_nodes/stats/indices/search", ClientUtil.HTTP_GET);// System.out.println(scrolls);}

通过串行运行和并行运行结果比较,并行处理的性能要好很多,实际检索到的文档数量等价一致。

5.参考文档

6.开发交流

elasticsearch技术交流群:166471282

elasticsearch微信公众号:

转载于:https://my.oschina.net/bboss/blog/1788729

你可能感兴趣的文章
小白学爬虫-批量部署Splash负载集群
查看>>
dubbo源码分析-服务端发布流程-笔记
查看>>
小菜鸡进阶之路-First week
查看>>
MyBatis之ResultMap标签
查看>>
kubernetes-1.11.0集群部署之master集群 (二)
查看>>
IDEA PermGen space内存溢出
查看>>
Create a RHEL6 PXE Installation Server
查看>>
【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!
查看>>
桌面支持--Office2013没有Office Picture Manage怎么安装
查看>>
chmod修改文件权限失败
查看>>
数据结构与算法-->互为素数
查看>>
Linux系统学习方法——写给小白
查看>>
Nginx服务器报500 Internal Server Error错误
查看>>
链表的游标实现
查看>>
Linux下查看CPU信息、机器型号等硬件信息命令
查看>>
Lync Server 2013 部署 _ 部署简介及系统要求
查看>>
前端小随笔
查看>>
view属性大全
查看>>
Java文件编码示例
查看>>
CactiFans V1.0中文版发布
查看>>