InfluxDB-Python: 无法按标签值查询
InfluxDB-Python: Unable to query by Tag value
我在使用 python 查询 InfluxDB 时遇到问题。
我有以下简单代码:
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)
我试图只获取具有所有字段的用户 Carol,但是通过上面的查询,我只收到一个空的 ResultSet({})。
我做错了什么?我什至用单引号试过了。
在此先感谢您的帮助!
您在代码中犯了两个错误:
- 您的 json 正文错误您需要将每个测量值放入 {} 并用 ,
分隔
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
}
}
]
- 您的标签值必须用单引号括起来:
query = client.query('''SELECT * FROM "brushEvents" WHERE "user"='Carol' ''')
另一种选择是使用参数绑定,这是首选方法:
query = 'SELECT * FROM "brushEvents" WHERE user=$user'
bind_params = {'user': 'Carol'}
result = client.query(query, bind_params=bind_params)
我在使用 python 查询 InfluxDB 时遇到问题。
我有以下简单代码:
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)
我试图只获取具有所有字段的用户 Carol,但是通过上面的查询,我只收到一个空的 ResultSet({})。
我做错了什么?我什至用单引号试过了。
在此先感谢您的帮助!
您在代码中犯了两个错误:
- 您的 json 正文错误您需要将每个测量值放入 {} 并用 , 分隔
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
}
}
]
- 您的标签值必须用单引号括起来:
query = client.query('''SELECT * FROM "brushEvents" WHERE "user"='Carol' ''')
另一种选择是使用参数绑定,这是首选方法:
query = 'SELECT * FROM "brushEvents" WHERE user=$user'
bind_params = {'user': 'Carol'}
result = client.query(query, bind_params=bind_params)