-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: Add documentation about how to connect to Influx Cloud (#22)
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |