Elasticsearch获取ES查询的所有结果,并批量导出Excel

xiaoxiao2021-02-28  67

工作环境是内网所以不能截图。

搭建了ELK环境。 3500W个dic中查询数据,并要求导出excel。

从es中查询 status=500,返回为空,查询时间超过2000ms的数据 head插件查询出索引的数据 sql更方便查询支持标准sql select param from logstash-sql—3p where numfounds=0 一、kibana画图 1.首先是用kibana画条状图,create index 之后 在discover中可以设置查询条件。右上角是时间设置,默认是15min。 2.discover中点击param,点击下面的add,然后返回的结果中就只剩param 3.visualize画图,filter作为x,count作为y轴 二、获取所有es的查询数据,并导出excel 1、es的size默认是10000 2、sql插件默认的size是200 所以用到了分页查询 之后又用了scroll 和mapreduce,有个es对应的api很方便没有条数的限制

首先呢,需要在java中引入elasticsearch-jar,比如使用maven:

org.elasticsearch elasticsearch 1.4.4 然后初始化一个client对象:

private static TransportClient client; private static String INDEX = “index_name”; private static String TYPE = “type_name”;

public static TransportClient init(){ Settings settings = ImmutableSettings.settingsBuilder() .put("client.transport.sniff", true) .put("cluster.name", "cluster_name") .build(); client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost",9300)); return client; } public static void main(String[] args) { TransportClient client = init(); //这样就可以使用client执行查询了 }

然后就是创建两个查询过程了 ,下面是from-size分页的执行代码:

System.out.println(“from size 模式启动!”); Date begin = new Date(); long count = client.prepareCount(INDEX).setTypes(TYPE).execute().actionGet().getCount(); SearchRequestBuilder requestBuilder = client.prepareSearch(INDEX).setTypes(TYPE).setQuery(QueryBuilders.matchAllQuery()); for(int i=0,sum=0; sum

转载请注明原文地址: https://www.6miu.com/read-52089.html

最新回复(0)