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()); }
相关推荐
newbornzhao 2020-09-14
做对一件事很重要 2020-09-07
renjinlong 2020-09-03
明瞳 2020-08-19
李玉志 2020-08-19
mengyue 2020-08-07
molong0 2020-08-06
AFei00 2020-08-03
molong0 2020-08-03
wenwentana 2020-08-03
YYDU 2020-08-03
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。
sifeimeng 2020-08-03
心丨悦 2020-08-03
liangwenrong 2020-07-31
sifeimeng 2020-08-01
mengyue 2020-07-30
tigercn 2020-07-29
IceStreamLab 2020-07-29