druid为各种场景提供了丰富的查询类型。 查询由各种JSON属性组合而成,不同类型的查询,JSON属性不同,下面主要介绍常用的查询类型。
1. Components
-
Datasources
一个数据源等价于druid表。此外,一个查询也可以作为数据源,提供类似于子查询的功能。查询数据源目前只支持GroupBy查询
-
Table Data Source
最常用的类型
123456789101112131415161718{
"queryType"
:
"scan"
,
"dataSource"
: {
"type"
:
"table"
,
"name"
:
"wikipedia3"
},
"resultFormat"
:
"list"
,
"columns"
: [
"page"
,
"countryName"
,
"cityName"
,
"countryIsoCode"
],
"intervals"
: [
"2016-06-27/2016-06-28"
],
"limit"
: 5
}
-
Union Data Source
联合数据源中的数据源必须拥有相同的schema。联合查询只能被发送给broker/router节点,不支持直接发送到历史节点
1234{
"type"
:
"union"
,
"dataSources"
: [
"<string_value1>"
,
"<string_value2>"
,
"<string_value3>"
, ... ]
}
-
-
Filters
-
Selector filter
等价于sql的
where countryIsoCode = 'US'
12345678910111213141516171819202122{
"queryType"
:
"scan"
,
"dataSource"
: {
"type"
:
"table"
,
"name"
:
"wikipedia3"
},
"filter"
: {
"type"
:
"selector"
,
"dimension"
:
"countryIsoCode"
,
"value"
:
"US"
},
"resultFormat"
:
"list"
,
"columns"
: [
"page"
,
"countryName"
,
"cityName"
],
"intervals"
: [
"2016-06-27/2016-06-28"
],
"limit"
: 5
}
-
Column Comparison filter
等价于sql的
where countryName = cityName
123456789101112131415161718192021222324{
"queryType"
:
"scan"
,
"dataSource"
: {
"type"
:
"table"
,
"name"
:
"wikipedia3"
},
"filter"
: {
"type"
:
"columnComparison"
,
"dimensions"
: [
"countryName"
,
"cityName"
]
},
"resultFormat"
:
"list"
,
"columns"
: [
"page"
,
"countryName"
,
"cityName"
],
"intervals"
: [
"2016-06-27/2016-06-28"
],
"limit"
: 5
}
-
Regular expression filter
正则表达式,支持标准的java正则表达式,下面的查询表示countryIsoCode以U开头
1234567891011121314151617181920212223{
"queryType"
:
"scan"
,
"dataSource"
: {
"type"
:
"table"
,
"name"
:
"wikipedia3"
},
"filter"
: {
"type"
:
"regex"
,
"dimension"
:
"countryIsoCode"
,
"pattern"
:
"^U"
},
"resultFormat"
:
"list"
,
"columns"
: [
"page"
,
"countryName"
,
"cityName"
,
"countryIsoCode"
],
"intervals"
: [
"2016-06-27/2016-06-28"
],
"limit"
: 5
}
-
Logical expression filters
支持and or not,下面的等价于
where countryIsoCode = 'US' and cityName = 'New York'
123456789101112131415161718192021222324252627282930313233{
"queryType"
:
"scan"
,
"dataSource"
: {
"type"
:
"table"
,
"name"
:
"wikipedia3"
},
"filter"
: {
"type"
:
"and"
,
"fields"
: [
{
"type"
:
"selector"
,
"dimension"
:
"countryIsoCode"
,
"value"
:
"US"
},
{
"type"
:
"selector"
,
"dimension"
:
"cityName"
,
"value"
:
"New York"
}
]
},
"resultFormat"
:
"list"
,
"columns"
: [
"page"
,
"countryName"
,
"cityName"
,
"countryIsoCode"
],
"intervals"
: [
"2016-06-27/2016-06-28"
],
"limit"
: 5
}
-
JavaScript filter
1234567891011121314151617181920212223{
"queryType"
:
"scan"
,
"dataSource"
: {
"type"
:
"table"
,
"name"
:
"wikipedia3"
},
"filter"
: {
"type"
:
"javascript"
,
"dimension"
:
"countryIsoCode"
,
"function"
:
"function(value) { return (value == 'US' || value == 'CN') }"
},
"resultFormat"
:
"list"
,
"columns"
: [
"page"
,
"countryName"
,
"cityName"
,
"countryIsoCode"
],
"intervals"
: [
"2016-06-27/2016-06-28"
],
"limit"
: 5
}
-
Search filter
用于部分字符串匹配,如下面的表示包含foo,并且对大小写不敏感
12345678910{
"filter"
: {
"type"
:
"search"
,
"dimension"
:
"product"
,
"query"
: {
"type"
:
"insensitive_contains"
,
"value"
:
"foo"
}
}
}
-
In filter
等价于
where countryIsoCode in ('US', 'CN')
1234567891011121314151617181920212223{
"queryType"
:
"scan"
,
"dataSource"
: {
"type"
:
"table"
,
"name"
:
"wikipedia3"
},
"filter"
: {
"type"
:
"in"
,
"dimension"
:
"countryIsoCode"
,
"values"
: [
"US"
,
"CN"
]
},
"resultFormat"
:
"list"
,
"columns"
: [
"page"
,
"countryName"
,
"cityName"
,
"countryIsoCode"
],
"intervals"
: [
"2016-06-27/2016-06-28"
],
"limit"
: 5
}
-
Like filter
等价于
where countryIsoCode like '%U'
1234567891011121314151617181920212223{
"queryType"
:
"scan"
,
"dataSource"
: {
"type"
:
"table"
,
"name"
:
"wikipedia3"
},
"filter"
: {
"type"
:
"like"
,
"dimension"
:
"countryIsoCode"
,
"pattern"
:
"%U"
},
"resultFormat"
:
"list"
,
"columns"
: [
"page"
,
"countryName"
,
"cityName"
,
"countryIsoCode"
],
"intervals"
: [
"2016-06-27/2016-06-28"
],
"limit"
: 5
}
-
Bound filter
等价于
"CN" < countryIsoCode < "US"
12345678910111213141516171819202122232425262728{
"queryType"
:
"scan"
,
"dataSource"
: {
"type"
:
"table"
,
"name"
:
"wikipedia3"
},
"filter"
: {
"type"
:
"bound"
,
"dimension"
:
"countryIsoCode"
,
"lower"
:
"CN"
,
"lowerStrict"
:
true
,
"upper"
:
"US"
,
"ordering"
:
"numeric"
,
"upperStrict"
:
true
,
"ordering"
:
"lexicographic"
},
"resultFormat"
:
"list"
,
"columns"
: [
"page"
,
"countryName"
,
"cityName"
,
"countryIsoCode"
],
"intervals"
: [
"2016-06-27/2016-06-28"
],
"limit"
: 5
}
-
-
Aggregations
-
Count aggregator
12345678select
page,
count
(*)
as
num
from
wikipedia3
where
"__time"
BETWEEN
TIMESTAMP
'2016-06-27 00:00:00'
AND
TIMESTAMP
'2016-06-28 00:00:00'
group
by
page
order
by
num
desc
limit 5
1234567891011121314151617{
"queryType"
:
"topN"
,
"dataSource"
:
"wikipedia3"
,
"dimension"
:
"page"
,
"threshold"
: 5,
"metric"
:
"num"
,
"granularity"
:
"all"
,
"aggregations"
: [
{
"type"
:
"count"
,
"name"
:
"num"
}
],
"intervals"
: [
"2016-06-27/2016-06-28"
]
}
-
Sum aggregators
longSum、doubleSum、floatSum
12345678select
page,
sum
(delta)
as
num
from
wikipedia3
where
"__time"
BETWEEN
TIMESTAMP
'2016-06-27 00:00:00'
AND
TIMESTAMP
'2016-06-28 00:00:00'
group
by
page
order
by
page
asc
limit 5
123456789101112131415161718{
"queryType"
:
"topN"
,
"dataSource"
:
"wikipedia3"
,
"dimension"
:
"page"
,
"threshold"
: 5,
"metric"
:
"num"
,
"granularity"
:
"all"
,
"aggregations"
: [
{
"type"
:
"longSum"
,
"name"
:
"num"
,
"fieldName"
:
"delta"
}
],
"intervals"
: [
"2016-06-27/2016-06-28"
]
}
-
Min / Max aggregators
doubleMin、doubleMax、floatMin、floatMax、longMin、longMax
12345678select
page,
max
(delta)
as
num
from
wikipedia3
where
"__time"
BETWEEN
TIMESTAMP
'2016-06-27 00:00:00'
AND
TIMESTAMP
'2016-06-28 00:00:00'
group
by
page
order
by
page
asc
limit 5
123456789101112131415161718{
"queryType"
:
"topN"
,
"dataSource"
:
"wikipedia3"
,
"dimension"
:
"page"
,
"threshold"
: 5,
"metric"
:
"num"
,
"granularity"
:
"all"
,
"aggregations"
: [
{
"type"
:
"longMax"
,
"name"
:
"num"
,
"fieldName"
:
"delta"
}
],
"intervals"
: [
"2016-06-27/2016-06-28"
]
}
-
First / Last aggregator
不能在数据摄入的时候使用,只能用于查询
Last:最大时间戳对应的数据,0 if no row exist;First最小时间戳对应的数据,0 if no row exist
-
JavaScript aggregator
-
-
Post Aggregations
对Aggregations的结果进行二次加工并输出,最终的结果既包含Aggregations的结果也包含Post Aggregations的结果
2. Timeseries
统计一段时间内的汇总数据
1
2
3
4
|
SELECT count (*) as num,
sum (added)
FROM wikipedia
WHERE "__time" BETWEEN TIMESTAMP '2016-06-27 00:00:00' AND TIMESTAMP '2016-06-27 23:59:59'
|
1
2
3
4
5
6
7
8
9
10
|
{ "queryType" : "timeseries" ,
"dataSource" : "wikipedia3" ,
"granularity" : "all" ,
"aggregations" : [
{ "type" : "count" , "name" : "count" },
{ "type" : "longSum" , "name" : "added" , "fieldName" : "added" }
],
"intervals" : [ "2016-06-27/2016-06-28" ]
} |
3. TopN
返回前N条数据,并可以按照metric排序,可以支持维度,但只有一个
1
2
3
4
5
6
7
8
|
SELECT page,
sum (added) as num
FROM wikipedia
WHERE "__time" BETWEEN TIMESTAMP '2016-06-27 00:00:00' AND TIMESTAMP '2016-06-27 23:59:59'
group by page
order by num desc
limit 5 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
{ "queryType" : "topN" ,
"dataSource" : "wikipedia3" ,
"dimension" : "page" ,
"threshold" : 5,
"metric" : "added" ,
"granularity" : "all" ,
"aggregations" : [
{
"type" : "doubleSum" ,
"name" : "added" ,
"fieldName" : "added"
}
],
"intervals" : [ "2016-06-27/2016-06-28" ]
} |
4. GroupBy
能对指定的多个维度分组,也支持对指定的维度排序,也支持limit,但是性能比TopN和Timeseries要差很多
1
2
3
4
5
6
7
8
9
10
|
SELECT page,
countryName,
sum (added) as num,
sum (delta) as num2
FROM wikipedia
WHERE "__time" BETWEEN TIMESTAMP '2016-06-27 00:00:00' AND TIMESTAMP '2016-06-27 23:59:59'
group by page,countryName
order by num desc
limit 5 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
{ "queryType" : "groupBy" ,
"dataSource" : "wikipedia3" ,
"granularity" : "all" ,
"dimensions" : [
"page" ,
"countryName"
],
"limitSpec" : {
"type" : "default" ,
"limit" : 5,
"columns" : [
{
"dimension" : "added" ,
"direction" : "descending" ,
"dimensionOrder" : {
"type" : "numeric"
}
}
]
},
"aggregations" : [
{
"type" : "longSum" ,
"name" : "added" ,
"fieldName" : "added"
},
{
"type" : "longSum" ,
"name" : "delta" ,
"fieldName" : "delta"
}
],
"intervals" : [
"2016-06-27/2016-06-28"
]
} |
5. Search
类似于like操作,可以查询多个维度列,不支持聚合
1
2
3
4
5
6
7
|
SELECT page, countryName FROM wikipedia
WHERE "__time" BETWEEN TIMESTAMP '2016-06-27 00:00:00' AND TIMESTAMP '2016-06-27 23:59:59'
and page like '%C' or countryName like '%C'
limit 5 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
{ "queryType" : "search" ,
"dataSource" : "wikipedia3" ,
"granularity" : "all" ,
"dimensions" : [
"page" ,
"countryName"
],
"query" : {
"type" : "insensitive_contains" ,
"value" : "C"
},
"sort" : {
"type" : "lexicographic"
},
"limit" : 5,
"intervals" : [
"2016-06-27/2016-06-28"
]
} |
6. Select
查数据,不支持聚合,但支持分页,排序
1
2
3
4
5
|
SELECT * FROM wikipedia
WHERE "__time" BETWEEN TIMESTAMP '2016-06-27 00:00:00' AND TIMESTAMP '2016-06-27 23:59:59'
limit 0,5 |
1
2
3
4
5
6
7
8
9
10
11
|
{ "queryType" : "select" ,
"dataSource" : "wikipedia3" ,
"granularity" : "all" ,
"dimensions" :[],
"metrics" :[],
"pagingSpec" :{ "pagingIdentifiers" : {}, "threshold" :5},
"intervals" : [
"2016-06-27/2016-06-28"
]
} |
7. Scan
类似于Select,但不支持分页,但是如果没有分页需求,推荐使用这个,性能比Select好
1
2
3
4
5
|
SELECT page,countryName FROM wikipedia
WHERE "__time" BETWEEN TIMESTAMP '2016-06-27 00:00:00' AND TIMESTAMP '2016-06-27 23:59:59'
limit 5 |
1
2
3
4
5
6
7
8
9
10
11
|
{ "queryType" : "scan" ,
"dataSource" : "wikipedia3" ,
"resultFormat" : "list" ,
"columns" :[ "page" , "countryName" ],
"intervals" : [
"2016-06-27/2016-06-28"
],
"batchSize" :20480,
"limit" :5
}
|
相关推荐
Druid是一个高效的数据查询系统,主要解决的是对于大量的基于时序的数据进行聚合查询。数据可以实时摄入,进入到Druid后立即可查,同时数据是几乎是不可变。通常是基于时序的事实事件,事实发生后进入Druid,外部...
Druid源码分析还要关注数据处理流程的实现细节,包括索引构建、索引加载、查询执行等关键路径分析。这需要对Druid的数据结构、处理流程、命名方式和组合规则有深入理解。 从系统特性的角度来看,Druid的设计注重...
1. **查询构建器**:这个功能允许用户构建复杂的Druid查询,无需直接编写JSON或SQL。用户可以选择不同的查询类型,如时间序列查询、TopN查询、搜索查询等,并设置相应的过滤条件和聚合函数。 2. **实时监控**:仪表...
当需要查询这些数据的时候,Druid 再从深度存储系统中将它们装载到内存供查询使用。 Druid 的主要特点包括列式存储格式、可扩展的分布式系统、大规模的并行处理、实时或批量摄取、自愈、自平衡、易操作、原生云、...
当前仅支持对Druid的查询。 将来会提供更多支持。 CLI支持以下选项: 选项 描述 --help 打印此帮助消息 --version 显示版本号 -v ,-- --verbose 显示正在进行的查询 -h ,-- --host 连接的主机 -s ,-- --...
Druid是大数据实时分析平台,能够处理大量数据的实时查询和分析。以下是大数据Druid集群实时分析搭建的详细步骤和知识点: 环境准备 * 3台ECS服务器,each with 4 cores, 8G memory, 100G disk * 2台ECS服务器,...
【标题】"Druid Monitor监控数据源和慢查询,还可以监控Web应用、URI监控、Session监控、Spring监控.zip" 提供了关于如何在SpringBoot应用中集成并利用Druid监控工具进行性能优化和问题排查的知识点。Druid是一个...
3. **数据分片与并行处理**:Druid支持数据的分片和并行查询,通过`com.alibaba.druid.sql.DruidSQLExecutor`类,可以将复杂的查询任务分解为多个子任务并发执行,提升查询速度。 4. **监控与日志**:Druid提供了...
Druid Monitor 不仅可以监控数据源和慢查询,还可以监控 Web 应用、URI 监控、Session 监控、Spring 监控等。 1. Druid Monitor 的监控能力 Druid Monitor 提供了强大的监控能力,可以监控数据源、慢查询、Web ...
Druid监控系统提供了一个Web界面,用于展示数据库连接池的各项指标,包括连接数量、SQL执行情况、慢查询记录等。它可以帮助我们了解数据库连接池的性能瓶颈,优化SQL执行效率,确保系统稳定运行。 **二、安装Druid...
Druid是一种非常流行的工具,用于对事件数据执行OLAP查询。 Druid现在在大多数组织中驱动实时仪表板。 我们@扎普爱德鲁伊! 因此,我们希望为使Druid变得更加友好而不断扩展的社区做出贡献。 我们希望简化与Druid...
Druid支持多种类型的查询,包括时间范围查询、过滤查询、聚合查询、TopN查询和JOIN操作。其内置的查询引擎能快速响应复杂查询,尤其适合OLAP(在线分析处理)场景。 6. **扩展性和可扩展性**: Druid可以通过水平...
它可以解析SQL语句并生成抽象语法树(AST),这使得Druid能够进行更深入的SQL优化,例如,通过设置SQL拦截器,可以实现SQL的动态改写,优化查询性能,或者添加审计日志等功能。 除此之外,Druid还支持多种数据源...
4. DruidStatService:维护全局的监控统计信息,提供监控数据的查询和展示。 5. SqlParser:Druid的SQL解析模块,用于解析SQL语句并生成相应的AST(抽象语法树)。 四、Druid源码学习价值 1. 了解数据库连接池...
Druid是一个功能强大且性能优异的数据源连接池,而PostgreSQL则是一种流行的开源关系型数据库管理系统。本教程将详细介绍如何在Java项目中使用Druid数据连接池连接PostgreSQL数据库,以实现一个简单的测试环境。 ...
Spring Data JPA是Spring Framework的一个模块,它简化了JPA的使用,提供了诸如查询生成、存储过程支持等功能。 使用JPA的步骤如下: 1. 添加依赖:在`pom.xml`文件中添加Spring Data JPA和所选数据库驱动的依赖。 ...
5. **集成与配置**:在Java项目中,我们可以将`druid-1.2.8.jar`添加到项目的类路径中,然后通过Spring或XML配置文件来初始化Druid数据源,设置连接池的大小、超时时间、验证查询等参数。 6. **使用示例**: ```...
2. **SQL拦截器**:支持自定义SQL解析,可以统计SQL执行的耗时,分析慢查询,帮助优化数据库性能。 3. **连接有效性检测**:Druid可以通过配置定期进行连接有效性检查,避免因长时间未使用的连接失效导致的问题。 ...
Druid的查询引擎设计用于高性能的实时分析,支持多种查询类型,如选择查询、过滤查询、聚合查询、排序和分组等。查询通过Broker节点路由到正确的数据存储节点,以提高响应速度。 5. **存储结构**: Druid采用列式...