pythoninfluxdbinfluxdb-python

InfluxDB-Python: Unable to query by Tag value


I have a problem regarding queries in InfluxDB with python.

I have the following simple code:

from influxdb import InfluxDBClient

client = InfluxDBClient(host='localhost', port=8086)



client.create_database('pyexample')

databases= client.get_list_database()

client.switch_database('pyexample')


json_body = [
    {
        "measurement": "brushEvents",
        "tags": {
            "user": "1",
            "brushId": "6c89f539-71c6-490d-a28d-6c5d84c0ee2f"
        },
        "time": "2018-03-28T8:01:00Z",
        "fields": {
            "duration": 127
        },
        "measurement": "brushEvents",
        "tags": {
            "user": "Carol",
            "brushId": "6c89f539-71c6-490d-a28d-6c5d84c0ee2r"
        },
        "time": "2018-03-28T8:01:00Z",
        "fields": {
            "duration": 129
        },
        "measurement": "brushEvents",
        "tags": {
            "user": "Evi",
            "brushId": "6c89f539-71c6-490d-a28d-6c5d84c0ee2e"
        },
        "time": "2018-03-28T8:01:00Z",
        "fields": {
            "duration": 132
        }
    }
    
]
client.write_points(json_body)

query = client.query('SELECT * FROM "brushEvents" WHERE "user"="Carol" ')


print(query)

I am trying to get only the user Carol with all fields, but with the query above, I am only receiving an empty ResultSet({}).

What I am doing wrong? I even tried it with single quotes.

Thanks in advance for your help!


Solution

  • You made two mistakes in your code:

    1. Your json body is wrong you need to put each measurement in {} and separate them with ,
    json_body = [
        {
            "measurement": "brushEvents",
            "tags": {
                "user": "1",
                "brushId": "6c89f539-71c6-490d-a28d-6c5d84c0ee2f"
            },
            "time": "2018-03-28T8:01:00Z",
            "fields": {
                "duration": 127
            }
        },
        {
            "measurement": "brushEvents",
            "tags": {
                "user": "Carol",
                "brushId": "6c89f539-71c6-490d-a28d-6c5d84c0ee2r"
            },
            "time": "2018-03-28T8:01:00Z",
            "fields": {
                "duration": 129
            }
        },
        {    
            "measurement": "brushEvents",
            "tags": {
                "user": "Evi",
                "brushId": "6c89f539-71c6-490d-a28d-6c5d84c0ee2e"
            },
            "time": "2018-03-28T8:01:00Z",
            "fields": {
                "duration": 132
            }
        }
    
    ]
    
    1. Your tag value has to be in single quotes:
    query = client.query('''SELECT * FROM "brushEvents" WHERE "user"='Carol' ''')
    

    Another option is to use parameter binding which is the preferred method:

    query = 'SELECT * FROM "brushEvents" WHERE user=$user'
    bind_params = {'user': 'Carol'}
    result = client.query(query, bind_params=bind_params)