如何为键/值对数组建模

niwlg2el  于 2021-07-15  发布在  ClickHouse
关注(0)|答案(1)|浏览(341)

我们使用clickhouse存储网页加载的内部性能指标。每个度量都包含一个键/值对数组,用于它们关心的自定义加载时间。我们希望将这些存储在clickhouse中,并能够像查询任何其他时间值一样查询时间。
例如,当我得到一个度量值,以及所有标准数据时,我可能会得到一些数据,这些数据会给我一堆自定义内容的加载时间,比如:

TimeStamp=1548268715
CustomEvents="a=10,b=20,c=30"

在本例中,我要存储值 a=10 , b=20 ,和 c=30 以这样的方式:
它仍然绑定到原始数据(因此我可以按时间戳、任何其他字段等进行过滤)。
我可以聚合和查询特定的“自定义事件”。例如,我可能想对所有事件做一个直方图 a 特定日期之间的时间值。
挑战在于我不知道存在哪些定制事件。我想我可以将这些列为白名单,但是它们的数量可能会变得非常大,而且定制事件的基数非常高。
如果你有任何想法,我将不胜感激。我有一些想法,但不介意任何想法。

tvokkenx

tvokkenx1#

clickhouse中的标准方法是使用嵌套结构,并从使用数组连接的结构中进行选择。
clickhouse中的underhood嵌套字段只是一组长度相同的数组。
样品:
创建这样的表

CREATE TABLE performance_metrics
(
    timestamp DateTime, 
    website String, 
    custom_events Nested (
        metric String,
        value UInt64  -- actually you can have more attributes here, if needed
    )
)
ENGINE = MergeTree
PARTITION BY toMonday(timestamp)
ORDER BY (website, timestamp);

将数据作为多个数组引用嵌套的子字段。这些数组的名称应以嵌套名称作为前缀,长度应相同:

INSERT INTO performance_metrics (timestamp, website, custom_events.metric, custom_events.value)  VALUES
( '2019-02-04 10:00:00', 'google.com',        ['a', 'b', 'c'],[10,20,30]),
( '2019-02-04 10:00:01', 'stackoverflow.com', ['b', 'c', 'd'],[22,29,40]),
( '2019-02-04 10:00:01', 'google.com',        ['a','d'], [8,42]);

现在您可以使用array join从性能指标中选择:

SELECT 
    website, 
    custom_events.metric, 
    median(custom_events.value), 
    min(timestamp), 
    max(timestamp)
FROM performance_metrics 
ARRAY JOIN custom_events
GROUP BY 
    website, 
    custom_events.metric
ORDER BY 
    website ASC, 
    custom_events.metric ASC

相关问题