Druid 为维度提取函数使用多个维度

Druid Using multiple dimensions for a Dimension Extraction Function

维度抽取函数是否可以使用多个维度?

类似于:

{
    "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 }"
    }
}

我认为这是不可能的。但是,您可以通过首先使用 virtualColumn“合并”2 个不同的维度,然后使用提取函数来创建类似的东西。然后您可以再次拆分这些值。

示例查询(使用 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"
            }
        ]
    }
}