Skip to content

Commit

Permalink
doc: Add documentation about how to connect to Influx Cloud (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar committed Oct 11, 2019
1 parent 27739bf commit aece506
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
79 changes: 79 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ InfluxDB 2.0 client features
- tasks management
- authorizations
- health check
- How To
- `Connect to InfluxDB Cloud`_
- `How to efficiently import large dataset`_
- `Efficiency write data from IOT sensor`_

Installation
------------
Expand Down Expand Up @@ -440,6 +444,81 @@ Efficiency write data from IOT sensor
.. marker-iot-end
Connect to InfluxDB Cloud
^^^^^^^^^^^^^^^^^^^^^^^^^
The following example demonstrate a simplest way how to write and query date with the InfluxDB Cloud.

At first point you should create an authentication token as is described `here <https://v2.docs.influxdata.com/v2.0/security/tokens/create-token/>`_.

After that you should configure properties: ``influx_cloud_url``, ``influx_cloud_token``, ``bucket`` and ``org`` in a ``influx_cloud.py`` examples.

The latest step is run python script via: ``python3 influx_cloud.py``.

.. code-block:: python
"""
Connect to InfluxDB 2.0 - write data and query them
"""
from datetime import datetime
from influxdb_client import Point, InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS
"""
Configure credentials
"""
influx_cloud_url = 'https://us-west-2-1.aws.cloud2.influxdata.com'
influx_cloud_token = '...'
bucket = '...'
org = '...'
client = InfluxDBClient(url=influx_cloud_url, token=influx_cloud_token)
try:
kind = 'temperature'
host = 'host1'
device = 'opt-123'
"""
Write data by Point structure
"""
point = Point(kind).tag('host', host).tag('device', device).field('value', 25.3).time(time=datetime.utcnow())
print(f'Writing to InfluxDB cloud: {point.to_line_protocol()} ...')
write_api = client.write_api(write_options=SYNCHRONOUS)
write_api.write(bucket=bucket, org=org, record=point)
print()
print('success')
print()
print()
"""
Query written data
"""
query = f'from(bucket: "{bucket}") |> range(start: 0) |> filter(fn: (r) => r._measurement == "{kind}")'
print(f'Querying from InfluxDB cloud: "{query}" ...')
print()
query_api = client.query_api()
tables = query_api.query(query=query, org=org)
for table in tables:
for row in table.records:
print(f'{row.values["_time"]}: host={row.values["host"]},device={row.values["device"]} '
f'{row.values["_value"]} °C')
print()
print('success')
except Exception as e:
print(e)
finally:
client.close()
.. marker-iot-end
Advanced Usage
--------------

Expand Down
60 changes: 60 additions & 0 deletions examples/influx_cloud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Connect to InfluxDB 2.0 - write data and query them
"""

from datetime import datetime

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

"""
Configure credentials
"""
influx_cloud_url = 'https://us-west-2-1.aws.cloud2.influxdata.com'
influx_cloud_token = 'a96jOZxh7iUhhZXRupKWGpLBhYnOVeP0hp1AWima2vQy5VCZPonvhHVa2NwpBpevIyQ96kLewS0jaLNC0kycnw=='
bucket = 'QSLHA'
org = 'jakub_bednar'

client = InfluxDBClient(url=influx_cloud_url, token=influx_cloud_token)
try:
kind = 'temperature'
host = 'host1'
device = 'opt-123'

"""
Write data by Point structure
"""
point = Point(kind).tag('host', host).tag('device', device).field('value', 25.3).time(time=datetime.utcnow())

print(f'Writing to InfluxDB cloud: {point.to_line_protocol()} ...')

write_api = client.write_api(write_options=SYNCHRONOUS)
write_api.write(bucket=bucket, org=org, record=point)

print()
print('success')
print()
print()

"""
Query written data
"""
query = f'from(bucket: "{bucket}") |> range(start: 0) |> filter(fn: (r) => r._measurement == "{kind}")'
print(f'Querying from InfluxDB cloud: "{query}" ...')
print()

query_api = client.query_api()
tables = query_api.query(query=query, org=org)

for table in tables:
for row in table.records:
print(f'{row.values["_time"]}: host={row.values["host"]},device={row.values["device"]} '
f'{row.values["_value"]} °C')

print()
print('success')

except Exception as e:
print(e)
finally:
client.close()

0 comments on commit aece506

Please sign in to comment.