clickhouse - 我可以在不经过物化视图的情况下在 AggregatingMergeTree 中插入聚合状态吗
问题描述
我一直在使用 AggregatingMergeTree 通过从原始数据表生成聚合状态的物化视图填充它们。这很好用。
我想知道是否有一种方法可以从我收到的数组中生成聚合状态,而无需通过每个数据点包含一行的表。例子:
我发现我可以通过这种方式从数组中生成聚合状态:
SELECT countState([1,2,3,4,5])
SELECT uniqState(['user1', 'user2', 'user3'])
# Does not work with quantiles
尽管我没有设法将它们直接插入聚合合并树中。例子:
CREATE TABLE state_test_agg(
id UInt64,
count_state AggregateFunction(count, UInt32),
uniq_state AggregateFunction(uniq, String)
)
ENGINE = AggregatingMergeTree()
ORDER BY (id)
INSERT INTO state_test_agg (id, count_state, uniq_state)
VALUES (1, countState([1,2,3,4]), uniqState(['a', 'b', 'b']))
Code: 184. DB::Exception: Aggregate function countState([1, 2, 3, 4]) is found in wrong place in query
相反,如果我首先将存储桶(数组)存储在一个表中并尝试将它们移动到我得到的聚合合并树中
CREATE TABLE state_test_raw(
id UInt64,
count_bucket Array(UInt64),
uniq_bucket Array(String)
)
ENGINE MergeTree
ORDER BY id
INSERT INTO state_test_raw (
id,
count_bucket,
uniq_bucket)
VALUES
(1, [1,2,3,4,5,5], ['a', 'b', 'c'])
-- so far so good
INSERT INTO state_test_agg
VALUES (id, count_state, uniq_state)
SELECT
id,
countState(count_bucket) as count_state,
uniqState(uniq_bucket) as uniq_state
FROM state_test_raw
GROUP BY id
Code: 47. DB::Exception: Missing columns: 'id' while processing query: 'id', required columns: 'id', source columns: '_dummy'
尚不清楚,尽管我认为这是一个问题,因为 Clickhouse 无法知道 id 列是否唯一。
有没有办法插入从数组而不是通过物化视图生成的聚合状态?
场景:我想记录指标并在其上构建汇总表。简单的方法是将每个数据点存储为 MergeTree 中的一行,然后创建一个物化视图,通过填充聚合合并树来生成汇总。例子:
timestamp, response_time, 200
timestamp2, response_time, 205
...
尽管这些数据点已经在源(因此是数组)中存储。
time_bucket_1, response_time, [200, 205]
我可以扩展它们并为每个数据点写入一行,但似乎我可以通过跳过该步骤并尝试直接从存储桶生成聚合状态来节省 Clickhouse 的存储和带宽,以及实现步骤使用的内存
谢谢。菲利波
解决方案
考虑使用arrayReduce将项目数组转换为聚合状态:
WITH
arrayReduce('countState', [1, 2, 3, 4, 5]) AS state_1,
arrayReduce('uniqState', ['user1', 'user2', 'user3', 'user1']) AS state_2
SELECT
countMerge(state_1) AS merge_1,
uniqMerge(state_2) AS merge_2
/*
┌─merge_1─┬─merge_2─┐
│ 5 │ 3 │
└─────────┴─────────┘
*/
INSERT INTO state_test_agg (id, count_state, uniq_state)
VALUES
(1,
arrayReduce('countState', [toUInt32(1),2,3,4]),
arrayReduce('uniqState', ['a', 'b', 'b'])),
(1,
arrayReduce('countState', [toUInt32(5),5]),
arrayReduce('uniqState', ['a', 'b', 'b','c']));
SELECT
id,
countMerge(count_state) AS c,
uniqMerge(uniq_state) AS u
FROM state_test_agg
GROUP BY id
/*
┌─id─┬─c─┬─u─┐
│ 1 │ 6 │ 3 │
└────┴───┴───┘
*/
推荐阅读
- java - 在创建休眠实体时是否总是需要有一个对象属性而不仅仅是 id?
- sanity - React 中的理智:我如何获取与一个项目相关的许多学生???(许多作者到 1 块)
- anylogic - 以编程方式更改 Seize-block 中的资源集数量
- c - 对 Tss2_TctiLdr_Initialize 的未定义引用
- google-bigquery - 传递一个数组
到 bigquery sql 中的 js UDF - reactjs - React Native - 关注列表中的第一个可触摸对象
- java - 如何指定@ForAll 非零整数?
- python - 如何在pycairo中正确填充弯曲形状的外部?
- java - 如何比较和重新排列数组列表
- angular - 键入'字符串 | 空 | undefined' 不可分配给类型 'string | 不明确的'