Reputation: 175
How can I iterate through the result set I get returned from influxDB? I get this result by using
client = InfluxDBClient(host=influx_host, port=influx_port,database='db')
q = client.query("select * from cpu limit 1")
ResultSet({'(u'cpu', None)': [{u'usage_guest_nice': 0, u'usage_user': 0.90783871790308868, u'usage_nice': 0, u'usage_steal': 0, u'usage_iowait': 0.056348610076366427, u'host': u'xxx.xxx.hostname.com', u'usage_guest': 0, u'usage_idle': 98.184322579062794, u'usage_softirq': 0.0062609566755314457, u'time': u'2016-06-26T16:25:00Z', u'usage_irq': 0, u'cpu': u'cpu-total', u'usage_system': 0.84522915123660536}]})
and I want to get the usage_user value, the usage_system value etc. and insert them in an array.
Upvotes: 1
Views: 12249
Reputation: 3573
As pointed out in the docs and by @peterdn, InfluxDBClient.query()
function returns a ResultSet
object.
Using rs.get_points()
will return a generator for all the points in the ResultSet.
Example:
rs = cli.query("SELECT * from cpu")
cpu_points = list(rs.get_points(measurement='cpu'))
You can even filter by measurement, tags or even both as follows:
Filter by measurement:
rs = cli.query("SELECT * from cpu")
cpu_points = list(rs.get_points(measurement='cpu'))
Filter by tags:
rs = cli.query("SELECT * from cpu")
cpu_influxdb_com_points = list(rs.get_points(tags={"host_name": "influxdb.com"}))
Filter by tags and measurement:
rs = cli.query("SELECT * from cpu")
points = list(rs.get_points(measurement='cpu', tags={'host_name': 'influxdb.com'}))
Hope this helps.
Upvotes: -1
Reputation: 4199
I concur wiht Peterdn as there is simpler way in client itself that goes like this
results = client.query("select * from cpu limit 100")
resultInList = results.get_points(measurement='cpu')
print(type(resultInList)
--->>> <class 'list'>
last print statement showing that you have list of response data.....it means it is converting your Resultset in list which you can easily iterate through without using Pandas...
Upvotes: 0
Reputation: 2466
The correct way to iterate through the ResultSet
is by using its get_points()
method, which can optionally filter by measurement or tags or both.
For example:
results = client.query("select * from cpu limit 1")
for measurement in results.get_points(measurement='cpu'):
usage_system = measurement['usage_system']
# do whatever with usage_system
See the official InfluxDB-Python documentation.
Upvotes: 3
Reputation: 175
I managed to export the results in JSON format with the raw option, then split them at commas, add them in the array, and iterate through them. I had to use the dataframes for this
from influxdb import InfluxDBClient
import pandas as pd
client = InfluxDBClient(host=influx_host, port=influx_port,database='db')
q = "select * from cpu limit 1"
result = pd.DataFrame(client.query(q, chunked=False).raw)
I hope this will help somebody else because the documentation is not very clear how to use this.
Upvotes: 0