Java中怎么查询druidAPI

Java中怎么查询druidAPI,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联专注于万柏林网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供万柏林营销型网站建设,万柏林网站制作、万柏林网页设计、万柏林网站官网定制、微信小程序服务,打造万柏林网络公司原创品牌,更为您提供万柏林网站排名全网营销落地服务。

maven依赖 in.zapr.druid druidry 2.1

1 组装json

//时间范围:Interval DateTime start = new DateTime(DateUtils.getToday()+"T00:00:00.000+08:00"); DateTime end = new DateTime(); Interval interval = new Interval(start,end); // 粒度 Granularity granularity = new SimpleGranularity(PredefinedGranularity.ALL); // 过滤 DruidFilter todayUvEventFilter = new SelectorFilter("event","3001"); DruidFilter todayUvPageIdFilter = new SelectorFilter("page_id","101"); // and过滤 DruidFilter todayUvFilters = new AndFilter(Arrays.asList(todayUvEventFilter,todayUvPageIdFilter)); // HyperUnique 聚合 DruidAggregator todayUvAggregator = new HyperUniqueAggregator("today_uv","user_id"); // Filteres聚合 DruidAggregator todayUvAggregators = new FilteredAggregator(todayUvFilters,todayUvAggregator);

DruidFilter articleUvEventFilter = new SelectorFilter("event","3001"); DruidFilter articleUvPageIdFilter = new SelectorFilter("page_id","102"); DruidFilter articleUvFilters = new AndFilter(Arrays.asList(articleUvEventFilter,articleUvPageIdFilter)); DruidAggregator articleUvAggregator = new HyperUniqueAggregator("article_uv","user_id"); DruidAggregator articleUvAggregators = new FilteredAggregator(articleUvFilters,articleUvAggregator);

DruidFilter ypsUvEventFilter = new SelectorFilter("event","3001"); DruidFilter ypsUvPageIdFilter = new SelectorFilter("page_id","201"); DruidFilter ypsUvFilters = new AndFilter(Arrays.asList(ypsUvEventFilter,ypsUvPageIdFilter)); DruidAggregator ypsUvAggregator = new HyperUniqueAggregator("yps_uv","user_id"); DruidAggregator ypsUvAggregators = new FilteredAggregator(ypsUvFilters,ypsUvAggregator);

DruidFilter messageUvButtonIdFilter = new SelectorFilter("button_id","10102"); DruidFilter messageUvFilters = new AndFilter(Arrays.asList(messageUvButtonIdFilter)); DruidAggregator messageUvAggregator = new HyperUniqueAggregator("message_uv","user_id"); DruidAggregator messageUvAggregators = new FilteredAggregator(messageUvFilters,messageUvAggregator);

// PostAggregator DruidPostAggregator articleUvArticleField = new HyperUniqueCardinalityPostAggregator("article_uv","article_uv"); DruidPostAggregator todayUvArticleField = new HyperUniqueCardinalityPostAggregator("today_uv","today_uv"); DruidPostAggregator articleRatePostAgg = ArithmeticPostAggregator.builder().name("article_rate").function(ArithmeticFunction.DIVIDE).fields(Arrays.asList(articleUvArticleField,todayUvArticleField)).build();

DruidPostAggregator ypsUvYpsField = new HyperUniqueCardinalityPostAggregator("yps_uv","yps_uv"); DruidPostAggregator todayUvYpsField = new HyperUniqueCardinalityPostAggregator("today_uv","today_uv"); DruidPostAggregator ypsRatePostAgg = ArithmeticPostAggregator.builder().name("yps_rate").function(ArithmeticFunction.DIVIDE).fields(Arrays.asList(ypsUvYpsField,todayUvYpsField)).build();

DruidPostAggregator messageUvMessageField = new HyperUniqueCardinalityPostAggregator("message_uv","message_uv"); DruidPostAggregator todayUvMessageField = new HyperUniqueCardinalityPostAggregator("today_uv","today_uv"); DruidPostAggregator messageRatePostAgg = ArithmeticPostAggregator.builder().name("message_rate").function(ArithmeticFunction.DIVIDE).fields(Arrays.asList(messageUvMessageField,todayUvMessageField)).build();

//组装DruidQuery DruidTimeSeriesQuery query = DruidTimeSeriesQuery.builder() .dataSource(druidProperties.getDataSourceOpenPlatform()) .granularity(granularity) .intervals(Collections.singletonList(interval)) .aggregators(Arrays.asList(todayUvAggregators,articleUvAggregators,ypsUvAggregators,messageUvAggregators)) .postAggregators(Arrays.asList(articleRatePostAgg,ypsRatePostAgg,messageRatePostAgg)) .build(); 2 查询 public List queryResult( DruidQuery query) throws ConnectionException, JsonProcessingException, QueryException { ObjectMapper mapper = new ObjectMapper(); String requiredJson = mapper.writeValueAsString(query); log.info(requiredJson); DruidClient client = dataSourceDruidConfig.druidClient(); client.connect(); List responses = client.query(query, HashMap.class); client.close(); return responses; } 3 分析并组装结果 try { List res = queryResult(query); if(res!=null){ DecimalFormat df = new DecimalFormat("0.00%"); for (HashMap hashMap : res){ Map event = (Map)hashMap.get("result");         //小数-》百分比 String yps_rate = String.valueOf(event.get("yps_rate")); entiretUvRateParam.setYps_rate(df.format(Double.parseDouble(yps_rate))); } } } catch (ConnectionException e) { e.printStackTrace(); return null; } catch (QueryException e) { e.printStackTrace(); return null; } catch (JsonProcessingException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; }

关于Java中怎么查询druidAPI问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


分享名称:Java中怎么查询druidAPI
链接分享:http://scyanting.com/article/gcgesh.html