java简单操作elasticsearch
1.基本过滤查询
long start = System.currentTimeMillis(); long end = start - 4 * 60 * 60 * 1000; RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("timestamp").from(end,true).to(start,true); QueryBuilder s=QueryBuilders.boolQuery().must(rangeQueryBuilder); QueryBuilder qb=new MatchAllQueryBuilder(); SearchResponse response= elasticsearchTemplate.getClient().prepareSearch("monitoring-cpu").setTypes("cloud-cpu").setQuery(s).setFrom(0) .setSize(100).get(); SearchHits searchHits = response.getHits(); for(SearchHit hit:searchHits.getHits()){ System.out.println(hit.getSourceAsString()); }
2.条件过滤,进然后行分组,对组内数据求平均,然后排行查询
//ES中查询所有主机的监控数据 BoolQueryBuilder uuidsBoolQuery = QueryBuilders.boolQuery(); uuidsBoolQuery.must(QueryBuilders.matchQuery("uuid", uuidStr)); //暂定向前推一天,计算平均 long end = System.currentTimeMillis(); long start = end - 24 * 60 * 60 * 1000; RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("timestamp").from(start,true).to(end,true); QueryBuilder timeFilter = QueryBuilders.boolQuery().must(rangeQueryBuilder); //开始cputop查询 //分组字段是id,排序由多个字段排序组成 TermsAggregationBuilder orderCpu = AggregationBuilders.terms("group-uuid").field("uuid.keyword").order(Terms.Order.compound( Terms.Order.aggregation("avg-cpuuse", true) )); //求和字段1 AvgAggregationBuilder avgCpu = AggregationBuilders.avg("avg-cpuuse").field("usage_idle"); orderCpu.subAggregation(avgCpu);//添加到分组聚合请求中 orderCpu.size(10);//top10限制 FilterAggregationBuilder cpuAggregationBuilder = AggregationBuilders.filter("uuidFilter", uuidsBoolQuery) .subAggregation(AggregationBuilders.filter("timeFilter",timeFilter).subAggregation(orderCpu)); SearchResponse response = elasticsearchTemplate.getClient().prepareSearch("monitoring-cpu").setTypes("cloud-cpu") .addAggregation(cpuAggregationBuilder) .get(); InternalFilter uuidFilterRe = response.getAggregations().get("uuidFilter"); InternalFilter timeFilterRe = uuidFilterRe.getAggregations().get("timeFilter"); Terms tms = timeFilterRe.getAggregations().get("group-uuid"); //遍历每一个分组的key for(Terms.Bucket tbb:tms.getBuckets()){ //获取count的和 InternalAvg avg = tbb.getAggregations().get("avg-cpuuse"); for (Map userResource : userResources) { Object uuid = userResource.get("uuid"); if (uuid != null && !"".equals(uuid.toString())){ if (uuid.equals(tbb.getKey())){ userResource.put("cupPercent",numberFormat.format(100.0 - avg.getValue())); cpuSort.add(userResource); } } } }
3.过滤聚合求平均查询
//ES中查询所有主机的监控数据 BoolQueryBuilder uuidsBoolQuery = QueryBuilders.boolQuery(); uuidsBoolQuery.must(QueryBuilders.matchQuery("uuid", "1,2,4")); //暂定向前推一天,计算平均 long end = System.currentTimeMillis(); long start = end - 24 * 60 * 60 * 1000; RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("timestamp").from(start,true).to(end,true); QueryBuilder timeFilter = QueryBuilders.boolQuery().must(rangeQueryBuilder); //开始查询Cpu平均使用率 FilterAggregationBuilder cpuAggregationBuilder = AggregationBuilders.filter("uuidFilter", uuidsBoolQuery) .subAggregation(AggregationBuilders.filter("timeFilter",timeFilter) .subAggregation(AggregationBuilders.avg("avgCpu").field("usage_idle"))); SearchResponse response = elasticsearchTemplate.getClient().prepareSearch("monitoring-cpu").setTypes("cloud-cpu") .addAggregation(cpuAggregationBuilder) .get(); InternalFilter uuidFilterRe = response.getAggregations().get("uuidFilter"); InternalFilter timeFilterRe = uuidFilterRe.getAggregations().get("timeFilter"); InternalAvg avgCpuRe = timeFilterRe.getAggregations().get("avgCpu"); String cpupercent = "0.00"; if (!"NaN".equals(avgCpuRe.getValue() + "")){ cpupercent = numberFormat.format(100.0 - avgCpuRe.getValue()); }
3.条件过滤查询并按照时间粒度进行分组,求平均
//ES指定的主机 BoolQueryBuilder uuidsBoolQuery = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("uuid", instanceId)); //对近一天时间的数据按照小时的粒度进行分组 long end = System.currentTimeMillis(); long start = end - 24 * 60 * 60 * 1000; RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("timestamp").from(start,true).to(end,true); QueryBuilder timeFilter = QueryBuilders.boolQuery().must(rangeQueryBuilder); DateHistogramAggregationBuilder dateAgg = AggregationBuilders.dateHistogram("time-group").field("timestamp") .dateHistogramInterval(DateHistogramInterval.HOUR); FilterAggregationBuilder queryFilter = AggregationBuilders.filter("uuidFilter", uuidsBoolQuery) .subAggregation(AggregationBuilders.filter("timeFilter",timeFilter) .subAggregation(dateAgg .subAggregation(AggregationBuilders.avg("avgMem").field("mem_used_percent")) .subAggregation(AggregationBuilders.avg("avgDisk").field("disk_used_percent")) .subAggregation(AggregationBuilders.avg("avgCpu").field("cpu_usage_idle")))); SearchResponse response = elasticsearchTemplate.getClient().prepareSearch(ES_MONITOR_INDEX).setTypes(ES_MONITOR_INDEX_TYPE) .addAggregation(queryFilter) .get(); InternalFilter uuidFilterRe = response.getAggregations().get("uuidFilter"); InternalFilter timeFilterRe = uuidFilterRe.getAggregations().get("timeFilter"); Histogram timeGroup = timeFilterRe.getAggregations().get("time-group"); for(Histogram.Bucket entry:timeGroup.getBuckets()){ System.out.println(entry.getKey()+"-----"+entry.getDocCount()); InternalAvg avgMem = entry.getAggregations().get("avgMem"); InternalAvg avgDisk = entry.getAggregations().get("avgDisk"); InternalAvg avgCpu = entry.getAggregations().get("avgCpu"); System.out.println("avgMem:==========" + avgMem.getValue()); //监控详情对象 }
欢迎关注,一个注在共享、交流的Java学习平台。