Search code examples
time-seriesinfluxdbinfluxdb-python

InfluxDB write points with same timestamp but different measurement


Requirement: I want to create an influxDB database to store time-series data from multiple sensors reporting temperatures from different locations.

Problem: When I write points to the database with same timestamp but different tags (example : location) and field (temperature) value , influx overwrites both tags and fields values with the latest timestamp

I followed the documentation available on their website and they show a sample db with above requirement but am not able find the schema used.

Example Table with duplicate timestamps

Additional Information : Sample Input :

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
    }
}]

I used the example given in official documentation , still instead of 2 records , I get only one. Please note host tag is different which should ideally make each point unique.

Documentation


Solution

  • Today I too faced the same issue and now I come to the solution. :)

    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')
    

    Here is the output

    > 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