是否可以从ClickHouse FORMAT JSON查询中排除“元”,“行”,“统计”字段?

时间:2019-11-18 07:47:35

标签: clickhouse

查询:

SELECT value FROM test.json_test FORMAT JSON

响应:

{
    "meta":
    [
        {
            "name": "value",
            "type": "Int32"
        }
    ],

    "data":
    [
        {
            "value": 1
        }
    ],

    "rows": 1,

    "statistics":
    {
        "elapsed": 0.112135109,
        "rows_read": 1,
        "bytes_read": 4
    }
}

如何排除不必要的字段并仅保留data字段?

4 个答案:

答案 0 :(得分:1)

您可以从输出中排除“统计”:

  

设置output_format_write_statistics = 0;

ubuntu-16gb-nbg1-1 :) select 1 format JSON;

SELECT 1
FORMAT JSON

{
    "meta":
    [
        {
            "name": "1",
            "type": "UInt8"
        }
    ],

    "data":
    [
        {
            "1": 1
        }
    ],

    "rows": 1
}

1 rows in set. Elapsed: 0.003 sec. 

答案 1 :(得分:0)

也许JSONEachRow将满足您的需求?

答案 2 :(得分:0)

JSONEachRow似乎不能直接使用,因为结果JSON无效。

您可以将数据分组到数组以获取有效的JSON:

SELECT
    groupArray(value) AS values,
    groupArray((value, name)) AS objects
FROM
(
    SELECT
        1 AS value,
        'str1' AS name
    UNION ALL
    SELECT
        2 AS value,
        'str2' AS name
)
FORMAT JSONEachRow

/* Result:
{"values":[1,2],"objects":[[1,"str1"],[2,"str2"]]}
*/

答案 3 :(得分:0)

您尝试在哪里运行查询? 也许您可以尝试通过管道和JQ过滤此输出

clickhouse-client -q "SELECT value FROM test.json_test FORMAT JSON" | jq .data