0

So I am new to InfluxDB (v1) and even newer to InfluxDB v2 and Flux . Please don't be an arse as I am really trying hard to get my python code working again.

Recently, I upgraded my flux database from v1.8 to 2.6. This has been an absolute challenge but I think I have things working for the most part. (At least inserting data back into the database.) Reading items out of the database, however, has been especially challenging as I can't get my python code to work.

This is what I previously used in my python code when I was running flux 1.8 and using FluxQL. Essentially I need to convert these FluxQL queries to FLUX and get the expected results

meterids = influx_client.query('show tag values with key ="meter_id"')
metervalues = influx_client.query('select last(reading) from meter_consumption group by \*;')

With flux v2.6 I must use FLUX queries. For 'meterids' I do the following and it seems to work. (This took me days to figure out.)

metervalues_list = []

query_api = influx_client.query_api()

querystr = 'from(bucket: "rtlamr_bucket") \
    |\> range(start: -1h)\
    |\> keyValues(keyColumns: \["meter_id"\])'  # this gives a bunch of meters ids but formatted like \[('reading', '46259044'),'reading', '35515159'),...\]
    result  = query_api.query(query=querystr)

for table in result:
    for record in table.records:
    meterid_list.append((record.get_value()))

print('This is meterids: %s' %(meterid_list))

But when I try to pull actual last readings / value for each meter_id (the meter_consumption) I can't seem to get any Flux query to work. This is what i currently have:

#metervalues = influx_client.query('select last(reading) from meter_consumption group by \*;')

querystrconsumption = 'from(bucket: "rtlamr_bucket")\
    |\> range(start: -2h)\
    |\> filter(fn:(r) =\> r.\_measurement == "meter_consumption")\
    |\> group(columns: \["\_time"\], mode: "by")\
    |\> last()'

resultconsumption  = query_api.query(query=querystrconsumption)

for tableconsumption in resultconsumption:
    for recordconsumption in tableconsumption.records:
        metervalues_list.append((record.get_value()))

print('\\n\\nThis is metervalues: %s' %(metervalues_list))

Not sure if this will help, but in v1.8 of influxdb these were my measurements, tags and fields:

  • Time: timestamp
  • Measurement: consumption <-- consumption is the "measurement name"
  • Key-Value Tags (meta): meter_id, meter_type
  • Key-Value Fields (data): <meter_consumption in gal, ccf, etc.>

Any thoughts, suggestions or corrections would be most greatly appreciated. Apologies if I am not using the correct terminology. I have tried reading tons of google articles but I can't seem to figure this one out. :(

Jose Otero
  • 11
  • 2

0 Answers0