How to use Python to iterate through influxDB results

Cheetara picture Cheetara · Feb 1, 2018 · Viewed 8.7k times · Source

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.

Answer

peterdn picture peterdn · Feb 15, 2018

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.