德鲁伊使用多维维度提取功能

  • 本文关键字:功能 提取 德鲁伊 druid
  • 更新时间 :
  • 英文 :


是否可以将多个维度用于维度提取功能?

类似:

{
"type": "extraction",
"dimension": ["dimension_1", "dimension_2"],
"outputName": "new_dimension",
"outputType": "STRING",
"extractionFn": {
"type": "javascript",
"function": "function(x, y){ // do sth with both x and y to return the result }"
}
}

我认为这是不可能的。但是,您可以先通过"merge"创建类似的内容。使用virtualColumn提取两个不同的维度,然后使用提取函数。然后,您可以再次拆分这些值。

示例查询(使用https://github.com/level23/druid-client)

$client = new DruidClient([
"router_url" => "https://your.druid"
]);
// Build a groupBy query.
$builder = $client->query("hits")
->interval("now - 1 hour/now")
->select("os_name")
->select("browser")
->virtualColumn("concat(os_name, ';', browser)", "combined")
->sum("hits")
->select("combined", "coolBrowser", function (ExtractionBuilder $extractionBuilder) {
$extractionBuilder->javascript("function(t) { parts = t.split(';'); return parts[0] + ' with cool ' + parts[1] ; }");
})
->where("os_name", "!=", "")
->where("browser", "!=", "")
->orderBy("hits", "desc")
;
// Execute the query.
$response = $builder->groupBy();

结果示例:

+--------+--------------------------------------------------+--------------------------+---------------------------+
| hits   | coolBrowser                                      | browser                  | os_name                   | 
+--------+--------------------------------------------------+--------------------------+---------------------------+
| 418145 | Android with cool Chrome Mobile                  | Chrome Mobile            | Android                   | 
| 62937  | Windows 10 with cool Edge                        | Edge                     | Windows 10                | 
| 27956  | Android with cool Samsung Browser                | Samsung Browser          | Android                   | 
| 9460   | iOS with cool Safari                             | Safari                   | iOS                       |     
+--------+--------------------------------------------------+--------------------------+---------------------------+ 

原生德鲁伊json查询:

{
"queryType": "groupBy",
"dataSource": "hits",
"intervals": [
"2021-10-15T11:25:23.000Z/2021-10-15T12:25:23.000Z"
],
"dimensions": [
{
"type": "default",
"dimension": "os_name",
"outputType": "string",
"outputName": "os_name"
},
{
"type": "default",
"dimension": "browser",
"outputType": "string",
"outputName": "browser"
},
{
"type": "extraction",
"dimension": "combined",
"outputType": "string",
"outputName": "coolBrowser",
"extractionFn": {
"type": "javascript",
"function": "function(t) { parts = t.split(";"); return parts[0] + " with cool " + parts[1] ; }",
"injective": false
}
}
],
"granularity": "all",
"filter": {
"type": "and",
"fields": [
{
"type": "not",
"field": {
"type": "selector",
"dimension": "os_name",
"value": ""
}
},
{
"type": "not",
"field": {
"type": "selector",
"dimension": "browser",
"value": ""
}
}
]
},
"aggregations": [
{
"type": "longSum",
"name": "hits",
"fieldName": "hits"
}
],
"virtualColumns": [
{
"type": "expression",
"name": "combined",
"expression": "concat(os_name, ';', browser)",
"outputType": "string"
}
],
"context": {
"groupByStrategy": "v2"
},
"limitSpec": {
"type": "default",
"columns": [
{
"dimension": "hits",
"direction": "descending",
"dimensionOrder": "lexicographic"
}
]
}
}

最新更新