当前位置 主页 > 网站技术 > 代码类 >

    java操作elasticsearch的案例解析

    栏目:代码类 时间:2019-10-29 18:07

    这篇文章主要介绍了java操作elasticsearch的案例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    到目前为止,我们一直都是使用RESTful风格的 API操作elasticsearch服务,但是通过我们之前的学习知道,elasticsearch提供了很多语言的客户端用于操作elasticsearch服务,例如:java、python、.net、JavaScript、PHP等。而我们此次就学习如何使用java语言来操作elasticsearch服务。在elasticsearch的官网上提供了两种java语言的API,一种是Java Transport Client,一种是Java REST Client。

    而Java REST Client又分为Java Low Level REST Client和Java High Level REST Client,Java High Level REST Client是在Java Low Level REST Client的基础上做了封装,使其以更加面向对象和操作更加便利的方式调用elasticsearch服务。

    官方推荐使用Java High Level REST Client,因为在实际使用中,Java Transport Client在大并发的情况下会出现连接不稳定的情况。

    那接下来我们就来看看elasticsearch提供的Java High Level REST Client(以下简称高级REST客户端)的一些基础的操作,跟多的操作大家自行阅读elasticsearch的官方文档:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html在官网上已经对高级REST客户端的各种API做了很详细的使用说明,我们这篇文章主要还是翻译官网上的内容,先让大家以更友好的中文文档方式入门,等大家熟悉了这些API之后在查阅官网。

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