Java--druidAPI查询
maven依赖
<dependency>
<groupId>in.zapr.druid</groupId>
<artifactId>druidry</artifactId>
<version>2.1</version>
</dependency>
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<HashMap> 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<HashMap> responses = client.query(query, HashMap.class);
client.close();
return responses;
}
3 分析并组装结果
try {
List<HashMap> res = queryResult(query);
if(res!=null){
DecimalFormat df = new DecimalFormat("0.00%");
for (HashMap hashMap : res){
Map<String,Object> event = (Map<String,Object>)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 MongoDB查询(二)复杂查询
前言 在上篇<java MongoDB查询(一)简单查询>中我们简单了解了下查询,但是仅仅有那些查询是不够用的,还需要复杂的查询,这篇就这点进行叙述. 1.数据结构 集合:firstCol ...
- Java DNS查询内部实现
源码分析 在Java中,DNS相关的操作都是通过通过InetAddress提供的API实现的.比如查询域名对应的IP地址: String dottedQuadIpAddress = InetAddre ...
- java数据库查询类
通用查询数据库辅助类,可实现任意查询语句的查询,还可以进行多结果集查询. 类的代码: package com.hongyuan.db; import java.math.BigDecimal; imp ...
- JAVA 快递查询接口API调用-快递鸟接口
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- Java JPA 查询实体部分字段
前言 相信大家在用Java JPA作为ORM的时候都会有这种困惑,就是某个表T我仅仅希望取到其中的A.B.C三个字段,可是jpa是通过Entity Class映射的方式组合查询结果的. 那么如何通过使 ...
- [Java] JavaMail 查询邮件
上两篇提到如何发送邮件:JavaMail 发送 html 格式.带附件的邮件, JavaMail 简单案例.本文再演示下,如何读取邮箱上的邮件. 邮件内容的数据格式种类比较多,用户直接阅读邮件比较常见 ...
- 使用java API查询java类
一.java API的下载地址 前面列举了常用的java类,但只是介绍了功能,具体详细的用法(比如要知道该类的属性和方法)要需要调用java的API(Application Program Inter ...
- java 异步查询转同步多种实现方式:循环等待,CountDownLatch,Spring EventListener,超时处理和空循环性能优化
异步转同步 业务需求 有些接口查询反馈结果是异步返回的,无法立刻获取查询结果. 正常处理逻辑 触发异步操作,然后传递一个唯一标识. 等到异步结果返回,根据传入的唯一标识,匹配此次结果. 如何转换为同步 ...
- java MongoDB查询(一)简单查询
前言 MongoDB的java驱动提供了查询的功能,查询条件也是bson对象,这篇就看下怎么进行简单的数据查询 1.数据结构 集合:firstCollection 数据内容: { "_id& ...
- java命令查询属性信息
System.getProperty("user.home")可以查询JAVA系统的user.home属性的值, 除了user.home,还有user.dir, file.sepa ...
随机推荐
- ajax发送列表(traditional, serialize )
$(function () { $('#add_submit_ajax').click(function () { $.ajax({ url:'/addapp/', data:$('#add_app' ...
- python之路:模块初识
python王者开发之路:模块初识 模块初识我现在讲的确有点早.不过没关系,后面我会详细说模块. 模块,也就是库,是python三剑客之一.这三剑客,函数.库和类,都是由程序编写而成的.之所以我先说模 ...
- eclipse/idea远程调试Linux程序
第一步.在Tomcat的bin目录下的startup.sh文件的倒数第二行增加“JPDA_ADDRESS=8787”,最后一行在start的前边增加“jpda”,之后重启Tomcat 第二步.配置Ec ...
- IBM MQ 使用指南
一.MQ基本操作 MQ中有几个很重要的组件:队列管理器(QueueManager).队列(Queue)和通道(Channel).其基本的操作方法如下: 1)创建队列管理器 crtmqm –q QMg ...
- Your branch and remoteBranchName have diverged solution
(zhuan)git pull时解决分支分叉(branch diverged)问题 git pull时出现分支冲突(branch diverged) $ git status # On branch ...
- 从 TFS 迁移源代码到 git
准备工具: https://github.com/git-tfs/git-tfs 具体的安装步骤上面的 readme.md 中有说明.通过 Chocolatey 安装,如果本地没有 git ,会自动安 ...
- vue项目结构搭建
1安装node.js,已集成npm 2.临时使用淘宝镜像 npm --registry https://registry.npm.taobao.org install express 3.instal ...
- PhoenixFD插件流体模拟——UI布局【Output】详解
Liquid Output 流体输出 本文主要讲解Output折叠栏中的内容.原文地址:https://docs.chaosgroup.com/display/PHX3MAX/Liquid+Outp ...
- ACM学习之路
2018-10-18 11:03:00 今天开始踏上实现梦想的道路,希望自己不要懈怠. 坚持做简单的事,坚持下来就会变得不简单.
- 关于jqGrid组件表格无法自适应宽度问题
今天生成了一个4列的表格,但是无法自适应宽度,使用 $(window).resize(function(){ $(window).unbind("onresize"); $(&qu ...