Inserting list as value in Influxdb

4.9k views Asked by At

Is it possible to somehow insert list as a value in InfluxDB? This is the json:

json_body = [
    {
        "measurement":"devices",
        "tags":{
            "host":"server01",
            "region":"us-west"

            },
        "fields": {


            "device":1234,
            "coord":[60.177751,24.913778],
            "local":[[244,5,'232E','F27B',23],[244,5,'232F','76FE',9]]


            }

        }
    ]

Alternative would be to use string representation of list but then I have to convert it to list like below, which works fine.

ast.literal_eval(device_points[0]['local'])

Here is the json object with string representations:

json_body = [
    {
        "measurement":"devices",
        "tags":{
            "host":"server01",
            "region":"us-west"

            },
        "fields": {


            "device":1234,
            "coord":"[60.177751,24.913778]",
            "local":"[[244,5,'232E','F27B',23],[244,5,'232F','76FE',9]]"


            }

        }
    ]

client.write_points(json_body)

query = 'select local from devices;'

print("Querying data: " + query)

result = client.query(query)

device_points = list(result.get_points(measurement='devices'))

Is there some other way to achieve writing of lists directly?

Thanks!

3

There are 3 answers

0
herberts On

You could probably store the list as a string, but then you would not have any way of manipulating it afterwards as influxdb is rather poor on string manipulation and lacks ways of extracting infos from strings and turn them into fields.

So yes, the above suggestion to split it into fields is probably your best option if you want to stick with influxdb.

0
Atul On

There are two ways to do it

  1. Convert the list to a string and store it as a field. In this method, the relative ordering of items in the list is preserved and you can later consume this string and get back the original list

  2. Take out individual items from the list make a tag set from it and store these points in influx. The demerit here is the order of items is lost

0
Michael Desa On

There isn't a way to store lists as fields in InfluxDB. A common work around is to split the vector components out into individual fields.