Python client library

    This guide presumes some familiarity with Python and InfluxDB. If just getting started, see Get started with InfluxDB.

    1. Install the InfluxDB Python library:

    2. Ensure that InfluxDB is running. If running InfluxDB locally, visit . (If using InfluxDB Cloud, visit the URL of your InfluxDB Cloud UI. For example: https://us-west-2-1.aws.cloud2.influxdata.com.)

    We are going to write some data in using the Python library.

    1. In your Python program, import the InfluxDB client library and use it to write data to InfluxDB.

      1. from influxdb_client.client.write_api import SYNCHRONOUS
      1. bucket = "<my-bucket>"
      2. org = "<my-org>"
      3. token = "<my-token>"
      4. # Store the URL of your InfluxDB instance
      5. url="http://localhost:8086"
    2. Instantiate the client. The InfluxDBClient object takes three named parameters: url, org, and token. Pass in the named parameters.

      1. client = influxdb_client.InfluxDBClient(
      2. url=url,
      3. token=token,
      4. org=org
      5. )

      The InfluxDBClient object has a write_api method used for configuration.

    3. Instantiate a write client using the client object and the write_api method. Use the write_api method to configure the writer object.

    4. Create a point object and write it to InfluxDB using the write method of the API writer object. The write method requires three parameters: bucket, org, and record.

      1. p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
      2. write_api.write(bucket=bucket, org=org, record=p)
    1. from influxdb_client.client.write_api import SYNCHRONOUS
    2. bucket = "<my-bucket>"
    3. org = "<my-org>"
    4. token = "<my-token>"
    5. # Store the URL of your InfluxDB instance
    6. url="http://localhost:8086"
    7. client = influxdb_client.InfluxDBClient(
    8. url=url,
    9. token=token,
    10. org=org
    11. )
    12. write_api = client.write_api(write_options=SYNCHRONOUS)
    13. p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
    14. write_api.write(bucket=bucket, org=org, record=p)
    1. Instantiate the query client.

      1. query_api = client.query_api()
    2. The query client sends the Flux query to InfluxDB and returns a Flux object with a table structure.

    3. Pass the query() method two named parameters:org and query.

      1. result = query_api.query(org=org, query=query)
    4. Iterate through the tables and records in the Flux object.

      • Use the get_value() method to return values.
      • Use the get_field() method to return fields.
      1. for record in table.records:
      2. results.append((record.get_field(), record.get_value()))
      3. print(results)
      4. [(temperature, 25.3)]

    The Flux object provides the following methods for accessing your data:

    • get_measurement(): Returns the measurement name of the record.
    • get_field(): Returns the field name.
    • get_value(): Returns the actual field value.
    • values: Returns a map of column values.
    • values.get("<your tag>"): Returns a value from the record for given column.
    • get_time(): Returns the time of the record.
    • get_start(): Returns the inclusive lower time bound of all records in the current table.
    • get_stop(): Returns the exclusive upper time bound of all records in the current table.

    Complete example query script

    1. query_api = client.query_api()
    2. query = from(bucket:"my-bucket")\
    3. |> range(start: -10m)\
    4. |> filter(fn:(r) => r._measurement == "my_measurement")\
    5. |> filter(fn: (r) => r.location == "Prague")\
    6. |> filter(fn:(r) => r._field == "temperature" )‘
    7. result = query_api.query(org=org, query=query)
    8. results = []
    9. for table in result:
    10. for record in table.records:
    11. results.append((record.get_field(), record.get_value()))

    For more information, see the Python client README on GitHub.