【问题标题】:How to query from an Influx database with an absent field?如何从缺少字段的 Influx 数据库中查询?
【发布时间】:2018-08-10 14:06:58
【问题描述】:

我有一个由 telegraf 收集的测量结果。结构如下:

名称:smart_device

fieldKey    fieldType
--------    ---------
exit_status integer
health_ok   boolean
read_error_rate integer
seek_error_rate integer
temp_c      integer
udma_crc_errors integer

当我查询这个数据库时,我可以这样做:

> select  * from smart_device where  "health_ok" = true limit 1
name: smart_device
time            capacity    device  enabled exit_status health_ok   host    model           read_error_rate seek_error_rate serial_no   temp_c  udma_crc_errors wwn
----            --------    ------  ------- ----------- ---------   ----    -----           --------------- --------------- ---------   ------  --------------- ---
15337409500 2000398934016   sda Enabled     0           true        osd21   Hitachi HDS722020ALA330    0        0       JK11A4B8JR2EGW  38  0       5000cca222e6384f

还有这个:

> select  * from smart_device limit 1
name: smart_device
time            capacity    device  enabled exit_status health_ok   host    model   read_error_rate seek_error_rate serial_no   temp_c  udma_crc_errors wwn
----            --------    ------  ------- ----------- ---------   ----    -----   --------------- --------------- ---------   ------  --------------- ---
1533046990                   sda            0                      osd21    

但是当我尝试过滤掉带有空 health_ok 的记录时,我得到空输出:

> select  * from smart_device where "health_ok"!= true 
> 

如何选择空(否?null?)health_ok 的度量?

【问题讨论】:

    标签: influxdb kapacitor


    【解决方案1】:

    不幸的是,目前没有办法使用 InfluxQL 做到这一点。 InfluxDB 是一种面向文档的数据库;这意味着测量的行可以有不同的模式。因此,对于一行的字段,没有 null 的概念;实际上这一行没有字段。例如假设测量中有 4 行 cost

    > select * from cost
    name: cost
    time                isok type value
    ----                ---- ---- -----
    1533970927859614000 true 1    100
    1533970938243629700 true 2    101
    1533970949371761100      3    103
    1533970961571703900      2    104
    

    如您所见,有两行 isok=true 和两行没有名为 isok 的字段;所以只有一种方法可以通过此查询选择具有isok 字段的行的时间:

    > select isok from cost
    name: cost
    time                isok
    ----                ----
    1533970927859614000 true
    1533970938243629700 true
    

    由于 InfluxQL 目前不支持 where 子句中的子查询,因此无法查询没有 isok 字段的行(如果 InfluxDB 支持这种类型的查询,您可以像这样查询 SELECT * FROM cost WHERE time NOT IN (SELECT isok FROM cost)

    【讨论】:

      【解决方案2】:

      这不完全是原始问题的答案,但我发现了 Kapacitor 的一个特殊技巧。

      如果此查询已由 kapacitor 执行,它 (kapacitor) 有一个特殊的节点 default 允许添加缺少的字段/标签和一些值。

      对于health_ok 查询,它看起来像这样(tickscript):

      var data = stream
          |from()
            .measurement('smart_device')
              |default()   
                 .field('health_ok', FALSE)
      

      这允许假设如果 health_ok 丢失,它是FALSE

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-24
        • 2020-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-13
        • 2021-04-25
        相关资源
        最近更新 更多