跳到正文

Having 过滤器(groupBy) ​

INFO

Apache® Druid 支持两种查询语言:Druid SQL 和原生查询。 本文档介绍的是原生查询语言。 关于 SQL 中可用的函数,请参阅 SQL 文档。

having 子句是一个 JSON 对象,它通过对聚合值指定条件,来确定 groupBy 查询应返回哪些行。

它本质上等同于 SQL 中的 HAVING 子句。

Apache® Druid 支持以下几种类型的 having 子句。

查询过滤器 ​

查询过滤器类型的 HavingSpec 允许在查询的 Having 部分使用所有 Druid 查询过滤器。

查询过滤器 HavingSpec 的语法如下:

json
{
    "queryType": "groupBy",
    "dataSource": "sample_datasource",
    ...
    "having":
        {
            "type" : "filter",
            "filter" : <any Druid query filter>
        }
}

例如,使用 selector 过滤器:

json
{
    "queryType": "groupBy",
    "dataSource": "sample_datasource",
    ...
    "having":
        {
            "type" : "filter",
            "filter" : {
              "type": "selector",
              "dimension" : "<dimension>",
              "value" : "<dimension_value>"
            }
        }
}

你可以使用 "filter" 类型的 HavingSpec,通过对 "__time" 列应用过滤器, 按结果行的时间戳进行过滤。

数值过滤器 ​

最简单的 having 子句是数值过滤器。 数值过滤器可以作为更复杂的布尔过滤器表达式的基础过滤器。

下面是一个 having 子句数值过滤器的示例:

json
{
    "queryType": "groupBy",
    "dataSource": "sample_datasource",
    ...
    "having":
        {
            "type": "greaterThan",
            "aggregation": "<aggregate_metric>",
            "value": <numeric_value>
        }
}

等于(Equal To) ​

equalTo 过滤器匹配聚合值等于指定值的行。 equalTo 过滤器的语法如下:

json
{
    "queryType": "groupBy",
    "dataSource": "sample_datasource",
    ...
    "having":
        {
            "type": "equalTo",
            "aggregation": "<aggregate_metric>",
            "value": <numeric_value>
        }
}

这等同于 HAVING <aggregate> = <value>。

大于(Greater Than) ​

greaterThan 过滤器匹配聚合值大于给定值的行。 greaterThan 过滤器的语法如下:

json
{
    "queryType": "groupBy",
    "dataSource": "sample_datasource",
    ...
    "having":
        {
            "type": "greaterThan",
            "aggregation": "<aggregate_metric>",
            "value": <numeric_value>
        }
}

这等同于 HAVING <aggregate> > <value>。

小于(Less Than) ​

lessThan 过滤器匹配聚合值小于指定值的行。 greaterThan 过滤器的语法如下:

json
{
    "queryType": "groupBy",
    "dataSource": "sample_datasource",
    ...
    "having":
        {
            "type": "lessThan",
            "aggregation": "<aggregate_metric>",
            "value": <numeric_value>
        }
}

这等同于 HAVING <aggregate> < <value>。

维度选择过滤器 ​

dimSelector ​

dimSelector 过滤器匹配维度值等于指定值的行。 dimSelector 过滤器的语法如下:

json
{
    "queryType": "groupBy",
    "dataSource": "sample_datasource",
    ...
    "having":
       {
            "type": "dimSelector",
            "dimension": "<dimension>",
            "value": <dimension_value>
        }
}

逻辑表达式过滤器 ​

AND ​

AND 过滤器的语法如下:

json
{
    "queryType": "groupBy",
    "dataSource": "sample_datasource",
    ...
    "having":
        {
            "type": "and",
            "havingSpecs": [
                {
                    "type": "greaterThan",
                    "aggregation": "<aggregate_metric>",
                    "value": <numeric_value>
                },
                {
                    "type": "lessThan",
                    "aggregation": "<aggregate_metric>",
                    "value": <numeric_value>
                }
            ]
        }
}

OR ​

OR 过滤器的语法如下:

json
{
    "queryType": "groupBy",
    "dataSource": "sample_datasource",
    ...
    "having":
        {
            "type": "or",
            "havingSpecs": [
                {
                    "type": "greaterThan",
                    "aggregation": "<aggregate_metric>",
                    "value": <numeric_value>
                },
                {
                    "type": "equalTo",
                    "aggregation": "<aggregate_metric>",
                    "value": <numeric_value>
                }
            ]
        }
}

NOT ​

NOT 过滤器的语法如下:

json
{
    "queryType": "groupBy",
    "dataSource": "sample_datasource",
    ...
    "having":
        {
        "type": "not",
        "havingSpec":
            {
                "type": "equalTo",
                "aggregation": "<aggregate_metric>",
                "value": <numeric_value>
            }
        }
}

基于 Apache License 2.0 许可证发布。