聚合为json格式 - Trino / Hive

问题描述 投票:0回答:1

我在 Jupyter Notebook 中使用 Trino 和 Hive。我想通过以下方式聚合一个表:

q = f"""
CREATE TABLE {aggregated_table} AS
WITH aggregated_data AS (
    SELECT
        id,
        date,
        -- Aggregate counts into JSON strings
        json_format(
            map_agg(column_A, COUNT(column_A))
        ) AS column_A_counts,
        AVG(column_B) AS column_B_avg,
        FIRST_VALUE(column_C) OVER (PARTITION BY id, date) AS column_C
    FROM
        {disaggregated_table}
    GROUP BY
        id, date
)
SELECT
    *
FROM
    aggregated_data
"""

%sql $q

我得到的错误是:

TrinoUserError(type=USER_ERROR, name=FUNCTION_NOT_FOUND, message="line ...: Unexpected parameters (map(varchar(5),bigint)) for function json_format. Expected: json_format(json)"

我该如何解决?

sql hive presto trino
1个回答
0
投票

json_format
需要JSON数据类型,您可以转换为它,例如:

cast(map_agg(column_A, COUNT(column_A)) as JSON)
© www.soinside.com 2019 - 2024. All rights reserved.