【问题标题】:Writing to influx db returns no error, but no data is stored写入 influxdb 不会返回错误,但不会存储任何数据
【发布时间】:2021-03-09 16:28:45
【问题描述】:

我正在尝试将一些传感器数据写入 InfluxDB。客户端没有返回错误,但没有数据访问数据库。你能帮我一把吗?我玩了标签,不同的,时间格式等等,但无济于事。我读了我能找到的东西,但不知何故我迷路了。 我正在使用 InfluxDB 1.8.3,

def create_dictionary_for_value(temperature,humidity):
    return [{
            "measurement": "kojiboxclimate",
            "tags": {
                "host": "server01",
                "region": "us-west"
            },
            "time": datetime.datetime.utcnow().isoformat(),
            "fields": {
                "temperature": temperature,
                "humidity": humidity
            }
    }]

def main():
    client = InfluxDBClient(HOST, PORT, USER, PASSWORD, DBNAME)
    retention_policy = 'my_policy'
    client.create_retention_policy(retention_policy, '3d', 3, default=True)

    while True:
        temperature, humidity = sensor.read()
        payload = create_dictionary_for_value(temperature, humidity)
        print("{}\n".format(payload))
        client.write_points(payload, retention_policy=retention_policy, protocol='json')
        result = client.query('select value from kojiboxclimate;')
        print("Result: {0}".format(result))
        time.sleep(60)

有效载荷的输出产生了我所期望的:

[{“字段”:{“温度”:20.164034485389493,“湿度”: 38.62821393148699},“标签”:{“主机”:“server01”,“区域”:“美国西部”},“时间”:“2020-11-26T19:00:15.042571”,“测量”: 'kojiboxclimate'}]

结果集是空的,db也是。

【问题讨论】:

    标签: python influxdb-python


    【解决方案1】:

    当我尝试你的代码时,它似乎对我有用,除了搜索查询。

    你尝试用这行代码查询你的数据:

    result = client.query('select value from kojiboxclimate;')
    

    通过这个,您尝试获取您的数据的字段value,该字段不存在。

    如果您想获取所有数据,您可以这样做:

    result = client.query('select * from kojiboxclimate;')
    

    如果你想得到例如温度可以用下面的代码sn -p:

    result = client.query('select temperature from kojiboxclimate;')
    

    【讨论】:

    • 我似乎遇到了一个复合问题。我在脚本中进行了编辑。同时我重新安装了库,我想它已经修复了。但与此同时,我没有得到我破坏了我的查询。所以非常感谢你的帮助。修复对重新安装的库的查询现在可以正常工作了。
    猜你喜欢
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多