目錄
- 概述
- 查詢分類:
-
- 1、聚合查詢(Aggregation Queries)
-
- 1.1 Timeseries
- 1.2 TopN(TopN queries)
- 1.3 GroupBy
- 2、中繼資料查詢(Metadata Queries)
-
- 2.1 時間範圍查詢(Time Boundary Queries)
- 2.2 Segments中繼資料查詢(Segment Metadata Queries)
- 2.3 資料源中繼資料查詢(Data Source Metadata Queries)
- 3、搜尋查詢(Search Queries)
概述
Druid查詢是通過HTTP REST方式發送查詢請求,查詢的描述寫在一個JSON檔案中,可以處理查詢請求的服務包括Broker、Historical和Realtime,這幾個服務節點都提供了相同的查詢接口,但一般是将查詢請求發送至Broker節點,由Broker節點根據查詢的資料源來轉發至Historical或者RealTime節點。
另外,目前已有很多開源的使用其他語言查詢Druid資料的包。具體可參考:http://druid.io/docs/latest/development/libraries.html
Druid自帶的JSON+HTTP的查詢方式,使用的資料源為lxw1234。
執行查詢(這裡指定的是Broker Node的位址):
curl -X POST 'http://node2:8092/druid/v2/?pretty' -H 'content-type: application/json' -d @query.json
Druid關于Query的官方文檔位址在:http://druid.io/docs/latest/querying/querying.html
查詢分類:
基本的查詢有三類:聚合查詢(Aggregation Queries)、中繼資料查詢(Metadata Queries)和搜尋查詢(Search Queries)。
- 聚合查詢(Aggregation Queries)
- Timeseries
- TopN
- GroupBy
- 中繼資料查詢(Metadata Queries)
- TimeBoundary
- SegmentMetadata
- DatasourceMetadata
- 搜尋查詢(Search Queries)
- Search
1、聚合查詢(Aggregation Queries)
聚合查詢就是名額資料根據一定的規則,在一個或多個次元上進行聚合。
分為三類:
- Timeseries
- TopN
- GroupBy
1.1 Timeseries
Timeseries查詢根據指定的時間區間及時間間隔進行聚合查詢,在查詢中還可以指定過濾條件,需要聚合的名額列、等。
timeseries 查詢包括如下的字段:
字段名 | 描述 | 是否必須 |
---|---|---|
queryType | 查詢類型,這裡隻有填寫timeseries查詢 | 是 |
dataSource | 要查詢的資料源 | 是 |
intervals | 查詢的時間範圍,預設是ISO-8601格式 | 是 |
granularity | 查詢結果進行聚合的時間粒度(時間間隔) | 是 |
aggregations | 聚合的類型、字段及結果顯示的名稱 | 是 |
postAggregations | 後期聚合 | 否 |
filter | 過濾條件 | 否 |
descending | 是否降序 | 否 |
context | 指定一些查詢參數 | 否 |
timeseries輸出每個時間粒度内指定條件的統計資訊,通過filter指定條件過濾,通過aggregations和postAggregations指定聚合方式。timeseries不能輸出次元資訊,granularity支援all,none,second,minute,hour,day,week,month,year等次元。
一個簡單的Timeseries查詢配置檔案如下:
{
"queryType": "timeseries",
"dataSource": "lxw1234",
"intervals": [ "2015-11-15/2015-11-18" ],
"granularity": "day",
"aggregations": [
{"type": "longSum", "fieldName": "count", "name": "total_count"}
]
}
運作結果:
Zero-filling:
一般情況下,使用Timeseries查詢按天彙總,而某一天沒有資料(被過濾掉了),那麼在結果中會顯示該天的彙總結果為0。比如上面的資料,假設2015-11-15這一天沒有符合條件的資料,那麼結果會變成:
{
"timestamp" : "2015-11-15T00:00:00.000Z",
"result" : {
"total_count" : 0
}
}
如果不希望這種資料出現在結果中,那麼可以使用context選項來去掉它,context是用來指定一些查詢參數,配置如下:
"context" : {
"skipEmptyBuckets": "true"
}
1.2 TopN(TopN queries)
TopN就是基于一個次元GroupBy,然後按照彙總後的名額排序,取TopN.
在Druid中,TopN查詢要比相同實作方式的GroupBy+Ordering效率快。
實作原理上,其實也就是分而治之,比如取Top10,由每個任務節點各自取Top10,然後統一發送至Broker,由Broker從各個節點的Top10中,再彙總出最終的Top10.
TopN 查詢包括如下的字段:
字段名 | 描述 | 是否必須 |
---|---|---|
queryType | 查詢類型,這裡隻有填寫timeseries查詢 | 是 |
dataSource | 要查詢的資料源 | 是 |
intervals | 查詢的時間範圍,預設是ISO-8601格式 | 是 |
granularity | 查詢結果進行聚合的時間粒度(時間間隔) | 是 |
dimension | 進行TopN查詢的次元,一個TopN查詢隻能有一個次元 | 是 |
threshold | TopN中的N值 | 是 |
metric | 進行統計并排序的metric | 是 |
aggregations | 聚合的類型、字段及結果顯示的名稱 | 是 |
postAggregations | 後期聚合 | 否 |
filter | 過濾條件 | 否 |
context | 指定一些查詢參數 | 否 |
一個簡單的TopN查詢配置檔案:
{
"queryType": "topN",
"dataSource": "lxw1234",
"granularity": "day",
"dimension": "cookieid",
"metric": "total_count",
"threshold" : 3,
"aggregations": [
{"type": "longSum", "fieldName": "count", "name": "total_count"}
],
"intervals": ["2015-11-17/2015-11-18"]
}
該查詢查出每天pv最多的Top 3 cookieid,查詢結果:
注意:metric:是TopN專屬
metric 配置方式:
"metric":"<metric_name>" 預設情況是升序排序的
"metric" : {
"type" : "numeric", //指定按照numeric 降序排序
"metric" : "<metric_name>"
}
"metric" : {
"type" : "inverted", //指定按照numeric 升序排序
"metric" : "<metric_name>"
}
"metric" : {
"type" : "lexicographic", //指定按照字典序排序
"metric" : "<metric_name>"
}
"metric" : {
"type" : "alphaNumeric", //指定按照數字排序
"metric" : "<metric_name>"
}
1.3 GroupBy
GroupBy聚合查詢就是在多個次元上,将名額聚合。Druid中建議,能用TimeseriesQueries和TopN實作的查詢盡量不要用GroupBy,因為GroupBy的性能要差一些。
// TODO
參考:http://lxw1234.com/archives/2015/11/561.htm
2、中繼資料查詢(Metadata Queries)
2.1 時間範圍查詢(Time Boundary Queries)
時間範圍查詢用來查詢一個資料源的最小和最大時間點。
{
"queryType" : "timeBoundary",
"dataSource": "lxw1234"
}
查詢結果:
[ {
"timestamp" : "2015-11-15T00:00:00.000+08:00",
"result" : {
"minTime" : "2015-11-15T00:00:00.000+08:00",
"maxTime" : "2015-11-18T23:59:59.000+08:00"
}
} ]
另外,還有個bound選項,用來指定傳回最大時間點還是最小時間點,如果不指定,則兩個都傳回:
{
"queryType" : "timeBoundary",
"dataSource": "lxw1234",
"bound": "maxTime"
}
此時隻傳回最大時間點:
[ {
"timestamp" : "2015-11-18T23:59:59.000+08:00",
"result" : {
"maxTime" : "2015-11-18T23:59:59.000+08:00"
}
} ]
2.2 Segments中繼資料查詢(Segment Metadata Queries)
Segments中繼資料查詢可以查詢到每個Segment的以下資訊:
- 列名
- Segment中所有列的基數(Cardinality),非STRING類型的列為null;
- 每個列的預計大小(Bytes);
- 該Segment的時間跨度;
- 列的類型;
- 該Segment的預估總大小;
- Segment ID;
查詢配置:
{
"queryType":"segmentMetadata",
"dataSource":"lxw1234",
"intervals":["2015-11-15/2015-11-19"]
}
查詢結果(隻取了一個Segment):
{
"id" : "lxw1234_2015-11-17T00:00:00.000+08:00_2015-11-18T00:00:00.000+08:00_2015-11-18T16:53:02.158+08:00_1",
"intervals" : [ "2015-11-17T00:00:00.000+08:00/2015-11-18T00:00:00.000+08:00" ],
"columns" : {
"__time" : {
"type" : "LONG",
"size" : 46837800,
"cardinality" : null,
"errorMessage" : null
},
"cookieid" : {
"type" : "STRING",
"size" : 106261532,
"cardinality" : 1134359,
"errorMessage" : null
},
"count" : {
"type" : "LONG",
"size" : 37470240,
"cardinality" : null,
"errorMessage" : null
},
"ip" : {
"type" : "STRING",
"size" : 63478131,
"cardinality" : 735562,
"errorMessage" : null
}
},
"size" : 272782823
}
2.3 資料源中繼資料查詢(Data Source Metadata Queries)
這個查詢隻是傳回該資料源的最後一次有資料進入的時間。
比如,查詢配置檔案:
{
"queryType" : "dataSourceMetadata",
"dataSource": "lxw1234"
}
結果為:
[ {
"timestamp" : "2015-11-18T23:59:59.000+08:00",
"result" : {
"maxIngestedEventTime" : "2015-11-18T23:59:59.000+08:00"
}
} ]
3、搜尋查詢(Search Queries)
select 類似于sql中select操作,select用來檢視druid中的存儲的資料,并支援按照指定過濾器和時間段檢視指定次元和metric,能通過descending字段指定排序順序,并支援分頁拉取,但不支援aggregations和postAggregations。
json 執行個體如下:
{
"queryType": "select",
"dataSource": "app_auto_prem_qd_pp3",
"granularity": "all",
"intervals": "1917-08-25T08:35:20+00:00/2017-08-25T08:35:20+00:00",
"dimensions": [
"status",
"is_new_car"
],
"pagingSpec":{
"pagingIdentifiers":{},
"threshold":
},
"context" : {
"skipEmptyBuckets" : "true"
}
}
相當于SQL語句