具有相同时间戳但度量不同的InfluxDB写点

时间:2019-02-21 18:05:59

标签: time-series influxdb influxdb-python

要求: 我想创建一个influxDB数据库来存储来自多个传感器的时间序列数据,这些传感器报告不同位置的温度。

问题: 当我使用相同的时间戳将标记写入数据库但标记(例如:location)和字段(温度)值不同时,influx将使用最新的时间戳覆盖标记和字段值

我遵循了他们网站上的文档,他们显示了具有上述要求的示例数据库,但是找不到所使用的架构。

Example Table with duplicate timestamps

其他信息: 样本输入:

json_body_1 = [
{
    "measurement": "cpu_load_short",
    "tags": {
        "host": "server02",
        "region": "us-west"
    },
    "time": "2009-11-10T23:00:00Z",
    "fields": {
        "Float_value": 0.7,
        "Int_value": 6,
        "String_value": "Text",
        "Bool_value": False
    }
},
{
    "measurement": "cpu_load_short",
    "tags": {
        "host": "server01",
        "region": "us-west"
    },
    "time": "2009-11-10T23:00:00Z",
    "fields": {
        "Float_value": 1.0,
        "Int_value": 2,
        "String_value": "Text",
        "Bool_value": False
    }
}]

我使用了官方文档中给出的示例,但仍然只有2条记录而不是2条记录。请注意,主机标签是不同的,理想情况下应使每个点唯一。

Documentation

2 个答案:

答案 0 :(得分:1)

今天我也遇到了同样的问题,现在我来解决这个问题。 :)

from influxdb import InfluxDBClient

client = InfluxDBClient(host='host name', port=8086, database='test_db',username='writer', password=Config.INFLUXDB_WRITE_PWD)

points = [{
    "measurement": "cpu_load_short",
    "tags": {
        "host": "server02",
        "region": "us-west"
    },
    "time": "2009-11-10T23:00:00Z",
    "fields": {
        "Float_value": 0.7,
        "Int_value": 6,
        "String_value": "Text",
        "Bool_value": False
    }
},
{
    "measurement": "cpu_load_short",
    "tags": {
        "host": "server01",
        "region": "us-west"
    },
    "time": "2009-11-10T23:00:00Z",
    "fields": {
        "Float_value": 1.0,
        "Int_value": 2,
        "String_value": "Text",
        "Bool_value": False
    }
}]

status = client.write_points(json_body_1, database='test_db', batch_size=10000, protocol='json')

这是输出

> select * from cpu_load_short;
name: cpu_load_short
time                Bool_value Float_value Int_value String_value host     region
----                ---------- ----------- --------- ------------ ----     ------
1257894000000000000 false      1           2         Text         server01 us-west
1257894000000000000 false      0.7         6         Text         server02 us-west

答案 1 :(得分:0)

您可以尝试通过以下方式(类似于此Getting Started with Python and InfluxDB)进行操作:

from influxdb import InfluxDBClient

influx_client = InfluxDBClient(
    host='hostname', port=8086, database='dbname')
influx_client.create_database('dbname')

points = [{
    "measurement": "cpu_load_short",
    "tags": {
        "host": "server02",
        "region": "us-west"
    },
    "time": "2009-11-10T23:00:00Z",
    "fields": {
        "Float_value": 0.7,
        "Int_value": 6,
        "String_value": "Text",
        "Bool_value": False
    }
},
{
    "measurement": "cpu_load_short",
    "tags": {
        "host": "server01",
        "region": "us-west"
    },
    "time": "2009-11-10T23:00:00Z",
    "fields": {
        "Float_value": 1.0,
        "Int_value": 2,
        "String_value": "Text",
        "Bool_value": False
    }
}]


influx_client.write_points(points)