I am new to InfluxDB moving from MySQL for this particular project. I have an energy and pool management system that I wrote in Python and I have successfully been able to write my data to InfluxDB via the InfluxDBClient for Python but I am having a problem reading the data back out again.
Basically, I need to be able to generate a query that returns a single number and I keep getting (what looks like) json back as my result that includes a bunch of other information. I say “what looks like” json because the result that I am getting back fails the json validator.
Here is my query from the cli:
> SELECT XXX340_ch4_w FROM energy ORDER by time DESC LIMIT 1
name: energy
time XXX340_ch4_w
---- ------------
1522164296000000000 -1101.375
Here is my python code:
def read_energy_data(db, measurement, device):
client = InfluxDBClient(pooldb.influx_host, pooldb.influx_port, pooldb.influx_user, pooldb.influx_password, db)
results = client.query(("SELECT %s from %s ORDER by time DESC LIMIT 1") % (device, measurement))
return results
Here are the results returned with that query:
ResultSet({'(u'energy', None)': [{u'XXX340_ch4_w': -1101.375, u'time': u'2018-03-27T15:28:00Z'}]})
What I need to be returned is just the -1101.375 and nothing else. When I attempt to feed it into the python json parser it fails and attempting to validate it via https://jsonlint.com/ also fails.
I also attempted this:
def read_energy_data(db, measurement, device):
client = InfluxDBClient(pooldb.influx_host, pooldb.influx_port, pooldb.influx_user, pooldb.influx_password, db)
results = client.query(("SELECT %s from %s ORDER by time DESC LIMIT 1") % (device, measurement))
watts_in_use = list(results.get_points(measurement=measurement))
return watts_in_use
Which resulted in this:
[{u'XXX340_ch4_w': -1101.375, u'time': u'2018-03-27T15:39:12Z'}]
So a little less data, but still not what I am looking for and still not properly formatted json preventing me from running it through the python json library to get my data.
I am hoping someone can point me in the right direction.
Thank You