Select 使用嵌套字典查询 kdb+ TSDB table

Select query on kdb+ TSDB table with nested dictionary

为了在 kdb+ TSDB 中存储按标签分组的基于时间的数据,我创建了一个包含 timestampvaltags 列的 table。标签是键值对,所以我使用字典将此类信息存储在 tags.

列中

创建 table 的查询:

myTable:([]timestamp:-12h$();val:-9h$();tags:());

插入一些示例数据的查询:

`myTable insert ("P"$"2015-11-30 13:10:45.126381"; 521.45117; `house`room!215 111)

如何获得215号房子的全部data/rows?我尝试了以下查询但没有成功(类型错误):

select from myTable where tags[`house]=215

感谢任何帮助。

要访问字典中的元素,您需要深度索引 - 在这种情况下需要处理 2 级嵌套。外索引是行数,内索引是字典键。

在 q 中,为了在深度 (elide) 建立索引,您省略了其中一个索引级别,在这种特殊情况下,查询将是:

q) select from myTable where tags[;`house]=215
    timestamp                     val      tags
    ----------------------------------------------------------
    2015.11.30D13:10:45.126381000 521.4512 `house`room!215 111

注意事项:如果每行中的 tags 字典不是同构的(相同的键 + 相同类型的值),您可能会为 'type 行抛出 'type 错误 tags 字典缺少 house 键,因为 Kdb+ 将 return 一个可能不是预期类型的​​空值。因此,可能需要使用匹配运算符 (~).

例如:

q) select from myTable where tags[;`house]~\:215
    timestamp                     val      tags
    ----------------------------------------------------------
    2015.11.30D13:10:45.126381000 521.4512 `house`room!215 111

请注意,已使用 each-left (\:)adverb here. See here 了解有关副词的更多详细信息。