colonelPanic
colonelPanic

Reputation: 31

How can I use the Python client library for influxdb to for queries? (InfluxDB OSS v1.8.10)

I learned how to download the client library and write to influxdb databases in influxdb OSS v1.8 at this link:

https://docs.influxdata.com/influxdb/v1.8/tools/api_client_libraries/#install-and-use-the-python-client-library

but I can't find out how to use it for querying. My concern is that this version of influx doesn't seem to use buckets, and the explanation on github:

https://github.com/influxdata/influxdb-client-python

only explains how to write and query with buckets. It includes the code that makes querying with v1.8 possible, but it doesn't explain how to use it. If anyone has any tips or resources that could help, please let me know!

Upvotes: 1

Views: 3842

Answers (1)

powersj
powersj

Reputation: 379

The Python Client libraries were developed using the InfluxDB Cloud and InfluxDB 2.0 APIs. Some of these APIs were backported to InfluxDB 1.8, but as you have seen, there are a few differences.

With InfluxDB 1.8, there are two key things to keep in mind:

  1. The "bucket" parameter is the database you wish to read from. For querying, all you need to do is specify the database name. When writing data, it can also take the retention policy via a string like database/retention_policy (e.g. testing/autogen).

  2. To query, your database will need Flux support enabled, which is disabled by default. See this option to enable it. If you are new to Flux, check out this basics page on how to build queries.

Below is a brief example of using the python client library to write a point and then query it back using a flux query:

from influxdb_client import InfluxDBClient, Point

username = ''
password = ''
database = 'testing'

with InfluxDBClient(url='http://localhost:8086', token=f'{username}:{password}', org='-') as client:
    with client.write_api() as writer:
        point = Point("mem").tag("host", "host1").field("used_percent", 25.43234543)
        writer.write(bucket=database, record=point)

    querier = client.query_api()
    tables = querier.query(f'from(bucket: \"{database}\") |> range(start: -1h)')
    for record in tables[0].records:
        print(f'{record.get_measurement()} {record.get_field()}={record.get_value()} {record.get_time()}')

Hope that helps!

Upvotes: 2

Related Questions