-
Notifications
You must be signed in to change notification settings - Fork 186
/
influxdb_18_example.py
28 lines (18 loc) · 873 Bytes
/
influxdb_18_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from influxdb_client import InfluxDBClient, Point
username = 'username'
password = 'password'
database = 'telegraf'
retention_policy = 'autogen'
bucket = f'{database}/{retention_policy}'
with InfluxDBClient(url='http://localhost:8086', token=f'{username}:{password}', org='-') as client:
with client.write_api() as write_api:
print('*** Write Points ***')
point = Point("mem").tag("host", "host1").field("used_percent", 25.43234543)
print(point.to_line_protocol())
write_api.write(bucket=bucket, record=point)
print('*** Query Points ***')
query_api = client.query_api()
query = f'from(bucket: \"{bucket}\") |> range(start: -1h)'
tables = query_api.query(query)
for record in tables[0].records:
print(f'#{record.get_time()} #{record.get_measurement()}: #{record.get_field()} #{record.get_value()}')