码迷,mamicode.com
首页 > 编程语言 > 详细

java简单操作elasticsearch

时间:2019-10-28 11:06:28      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:tms   int   gre   amp   rms   str   数据   ber   keyword   

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());
        }

 

java简单操作elasticsearch

标签:tms   int   gre   amp   rms   str   数据   ber   keyword   

原文地址:https://www.cnblogs.com/kenmeon/p/11750732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!