Influx DB - Flux query

Im new to InfluxDB, I use influx-client-python to write some data.

client = InfluxDBClient(url="http://localhost:8086", token=token)
write_api = client.write_api(write_options=SYNCHRONOUS)

data = [
    {
        "measurement": "meter",
        "time": pendulum.parse("2020-01-28T06:00:00+01:00"),
        "tags": {"number": "GI000001"},
        "fields": {"index": 2003.0},
    },
    {
        "measurement": "meter",
        "time": pendulum.parse("2020-02-28T06:00:00+01:00"),
        "tags": {"number": "GI000001"},
        "fields": {"index": 2004.0},
    },
]

write_api.write(bucket, org, data)

When I try to quering, only the 1st line is shown. What wrong with my query ?

from(bucket: "MeterData")
  |> range(start: 0)
  |> filter(fn: (r) => r["_measurement"] == "meter")
  |> filter(fn: (r) => r["_field"] == "index")
  • InfluxDB version: 2.0.0-rc.3
  • InfluxDB-python version: 1.9.0
  • Python version: 3.7.1

1 Like

Hi @Dilip_K,

I just tried your code and everything works correct. My Python script:

import pendulum

from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS

bucket = "my-bucket"
org = "my-org"

client = InfluxDBClient(url="http://localhost:8086", token="my-token")

"""
Query
"""
write_api = client.write_api(write_options=SYNCHRONOUS)
data = [
    {
        "measurement": "meter",
        "time": pendulum.parse("2020-01-28T06:00:00+01:00"),
        "tags": {"number": "GI000001"},
        "fields": {"index": 2003.0},
    },
    {
        "measurement": "meter",
        "time": pendulum.parse("2020-02-28T06:00:00+01:00"),
        "tags": {"number": "GI000001"},
        "fields": {"index": 2004.0},
    },
]
write_api.write(bucket, org, data)

"""
Query
"""
query_api = client.query_api()
query = f'from(bucket: "{bucket}") |> range(start: 0) |> filter(fn: (r) => r["_measurement"] == "meter") |> filter(fn: (r) => r["_field"] == "index")'
tables = query_api.query(query, org=org)
for record in tables[0].records:
    print(f'{record.get_time()} {record.get_measurement()}: {record.get_field()} {record.get_value()}')

client.close()

Could you share debug output from the client?

client = InfluxDBClient(url="http://localhost:8086", token="my-token", debug=True)

Regards

Thanks you for your replay @bednar.

Here the output with debug=True:

send: b'POST /api/v2/write?org=my-org&bucket=my-bucket&precision=ns HTTP/1.1\r\nHost: localhost:8086\r\nAccept-Encoding: identity\r\nContent-Length: 109\r\n
Content-Encoding: identity\r\nContent-Type: text/plain\r\nAccept: application/json\r\nAuthorization: Token my-token\r\nUser-Agent: influxdb-client-python/1.12.0\r\n\r\n'                                                                                       
send: b'meter,number=GI000001 index=2003.0 1580187600000000000\nmeter,number=GI000001 index=2004.0 1582866000000000000'                                                 
reply: 'HTTP/1.1 204 No Content\r\n'                                                                                                                                    
header: Date: Fri, 20 Nov 2020 12:23:55 GMT
                                                                                                                    
send: b'POST /api/v2/query?org=my-orgHTTP/1.1\r\nHost: localhost:8086\r\nAccept-Encoding: identity\r\nContent-Length: 311\r\nAccept: application/json\r\nCon
tent-Type: application/json\r\nAuthorization: Token my-token\r\nUser-Agent: influxdb-cli
ent-python/1.12.0\r\n\r\n'                                                                                                                                              
send: b'{"query": "from(bucket: \\"my-bucket\\") |> range(start: 0) |> filter(fn: (r) => r[\\"_measurement\\"] == \\"meter\\") |> filter(fn: (r) => r[\\"_field\\"] == 
\\"index\\")", "dialect": {"header": true, "delimiter": ",", "annotations": ["datatype", "group", "default"], "commentPrefix": "#", "dateTimeFormat": "RFC3339"}}'      
reply: 'HTTP/1.1 200 OK\r\n'                                                                                                                                            
header: Content-Type: text/csv; charset=utf-8                                                                                                                           
header: Vary: Accept-Encoding                                                                                                                                           
header: Date: Fri, 20 Nov 2020 12:23:55 GMT                                                                                                                             
header: Transfer-Encoding: chunked
                                                                                                                                      
2020-01-28 05:00:00+00:00 meter: index 2003.0                                                                                                                           

Regards

The same script work on InfluxDB Cloud version b31d06d, I can see the two lines of data, but not in localhost ( OSS Version 2.0.0 (f46a3bd))

I juste updated my InfluxDB version to 2.0.2 and this worked for me :grinning:

Thank you

2 Likes

I am glad to hear that it works

1 Like