Writing list of dictionaries to Influxdb

937 Views Asked by At

I have a list of dictionaries as follows:

[{"vins": "50EA1LGA5KA900001", "use": "abc", "owner": "Jack"}, {"vins": "50EA1LGA0KA900004", "use": "xyz", "owner": "Laura"}, {"vins": "50EA1LGA2KA900005", "use": "pqr", "owner": "Sam"}]

I want to write the above dictionary by creating a new influx measurement called 'vin_info' as follows: The output I expect is :

vins | use | owner
50EA1LGA5KA900001 | abc | Jack
50EA1LGA0KA900004 | xyz | Laura
50EA1LGA2KA900005 | pqr | Sam

The code I tried so far to write my data is:

 data =   [{"vins": "50EA1LGA5KA900001", "use": "abc", "owner": "Jack"}, {"vins": "50EA1LGA0KA900004", "use": "xyz", "owner": "Laura"}, {"vins": "50EA1LGA2KA900005", "use": "pqr", "owner": "Sam"}]
 db = 'abc'
 query = 'select * from vin_info'
    client = InfluxDBClient(host='*****', port=8086,database=db)
        print("Connection Established")
        client.write_points(data)
        result = client.query(query)
        print(result)

But getting the following error:

AttributeError: 'str' object has no attribute 'get'

I am not sure how to insert the above data into influx measurement. Can someone please help me here.

Thanks in advance!

1

There are 1 best solutions below

0
On
query = 'select * from vin_info'

You are calling the measurement vin_info, which doesn't appear in the data.

Try this:

influx_data = []

for x in data:
    new_json = {}
    new_json.update("measurement", "vin_info")
    new_json.update("tags",x)

influx_data.append(new_json)

You should see all tags when you select the measurement.

Also, according to influxDBClient doc, query return a table.

So try this also:

tables = query_api.query(query)

for table in tables:
    print(table)