从es中查询数据滚动写入excel中
2018-08-02 17:15
92 查看
/** * 滚动输出 */ @Test public void scroll() throws IOException { int size = 10000; //设置每次滚动输出条数 String path = "E:\\outputTest\\scrollOutputTest.xls"; long startTime = System.currentTimeMillis(); //获取开始时间 SearchResponse response = client.prepareSearch("twitter").setTypes("tweet") .addSort(SortBuilders.fieldSort("_doc")) .setSize(size).setScroll(new TimeValue(2000)).execute() .actionGet(); //获取总数量 long totalCount = response.getHits().getTotalHits(); int page = (int) totalCount / size; System.out.println("totalCount:" + totalCount); HSSFWorkbook workbook = new HSSFWorkbook(); //创建表 HSSFSheet sheet = workbook.createSheet("test");//创建sheet页 //把第一行字段行写入sheet createFirstRow(response,sheet); //把数据写入sheet createOtherRow(response,sheet); for (int i = 0; i < page-1; i++) { //再次发送请求,并使用上次搜索结果的ScrollId response = client.prepareSearchScroll(response.getScrollId()) .setScroll(new TimeValue(20000)).execute() .actionGet(); createOtherRow(response,sheet); } writeData(workbook,path); long endTime = System.currentTimeMillis(); //获取结束时间 System.out.println("运行时间:"+(endTime-startTime)+"ms"); }
/** *构造首行的结构,未设置字段类型 */ private void createFirstRow(SearchResponse response,HSSFSheet sheet){ if(response.getHits().getTotalHits() !=0){ HSSFRow row = sheet.createRow(0); //创建行 Map<String,Object> map = response.getHits().getAt(0).getSourceAsMap(); int k=0; HSSFCell cell; for (Map.Entry<String, Object> entry : map.entrySet()) { cell = row.createCell(k); //创建单元格 cell.setCellValue(entry.getKey()); k++; } } }
/** *把数据写入sheet */ private void createOtherRow(SearchResponse response,HSSFSheet sheet) throws IOException { for(int i = 0;i<response.getHits().getHits().length;i++){ Map<String,Object> map = response.getHits().getAt(i).getSourceAsMap(); HSSFRow row1 = sheet.createRow(i + 1); int k = 0; for (Map.Entry<String, Object> entry : map.entrySet()) { row1.createCell(k).setCellValue(entry.getValue().toString()); k++; } } }
/** *导出数据到本地 */ private void writeData(HSSFWorkbook workbook,String path){ try { FileOutputStream fos = new FileOutputStream(path); workbook.write(fos); System.out.println("写入成功"); fos.close(); } catch (IOException e) { e.printStackTrace(); } }阅读更多
相关文章推荐
- 查询数据写入excel并下载
- Excel 中使用SQL 语句查询数据
- 利用es-head 删除指定查询条件中的数据
- MySQL之按月拆分主表并按月分表写入数据提高数据查询速度
- Excel 中使用SQL 语句查询数据(二)
- EXCEL读取与写入数据的最佳方案(个人认为)
- excel在android读取,写入,更新数据
- oracle查询数据,以asci写入文件
- 从Mysql查询的结果,写入txt文件中,每个字段用|分隔,下一条数据要换行
- 利用POI在Excel文档任意单元格写入数据
- [转贴]EXCEL读取与写入数据的最佳方案
- Elasticsearch获取ES查询的所有结果,并批量导出Excel
- NPOI 创建Excel,数据读取与写入
- 从数据库查询到的数据导出到excel中
- 导出EXCEL表格之三将数据库中查询出的数据原样导出
- NPOI大数据分批写入同个Excel
- jxl将html表单中数据写入到excel
- 查询EXCEL数据时,列数据为空的问题
- TestComplete ADO连接SQL数据库,查询数据导出到EXCEL
- poi读取数据写入excel