Skip to content

Commit 73689e1

Browse files
committed
docs: Querying
1 parent d1909c5 commit 73689e1

File tree

2 files changed

+67
-21
lines changed

2 files changed

+67
-21
lines changed

MIGRATION_GUIDE.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Please take a moment to review the following client docs:
2323
- [Dictionary-style object](#writing-dictionary-style-object)
2424
- [Structured data](#writing-structured-data)
2525
- [Pandas DataFrame](#writing-pandas-dataframe)
26+
- [Querying](#querying)
2627

2728
## Initializing Client
2829

@@ -285,9 +286,36 @@ with InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org')
285286
**influxdb-python**
286287

287288
```python
289+
from influxdb import InfluxDBClient
290+
291+
client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='dbname')
292+
293+
points = client.query('SELECT * from cpu').get_points()
294+
for point in points:
295+
print(point)
288296
```
289297

290298
**influxdb-client-python**
291299

292300
```python
301+
from influxdb_client import InfluxDBClient
302+
303+
with InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org', debug=True) as client:
304+
query = '''from(bucket: "my-bucket")
305+
|> range(start: -10000d)
306+
|> filter(fn: (r) => r["_measurement"] == "cpu")
307+
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
308+
'''
309+
310+
tables = client.query_api().query(query)
311+
for record in [record for table in tables for record in table.records]:
312+
print(record.values)
313+
```
314+
315+
If you would like to omit boilerplate columns such as `_result`, `_table`, `_start`, ... you can filter the record values by following expression:
316+
317+
```python
318+
print({k: v for k, v in record.values.items() if k not in ['result', 'table', '_start', '_stop', '_measurement']})
293319
```
320+
321+
For more info see [Flux Response Format](https://github.com/influxdata/flux/blob/master/docs/SPEC.md#response-format).

examples/buckets_management.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
1-
from influxdb_client import InfluxDBClient
2-
from influxdb_client.client.write_api import SYNCHRONOUS
1+
"""
2+
How to How to create, list and delete Buckets.
3+
"""
34

4-
with InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org') as client:
5-
write_api = client.write_api(write_options=SYNCHRONOUS)
5+
from influxdb_client import InfluxDBClient, BucketRetentionRules
66

7-
record = [
8-
{
9-
"measurement": "cpu_load_short",
10-
"tags": {
11-
"host": "server01",
12-
"region": "us-west"
13-
},
14-
"time": "2009-11-10T23:00:00Z",
15-
"fields": {
16-
"Float_value": 0.64,
17-
"Int_value": 3,
18-
"String_value": "Text",
19-
"Bool_value": True
20-
}
21-
}
22-
]
7+
"""
8+
Define credentials
9+
"""
10+
url = "http://localhost:8086"
11+
token = "my-token"
12+
org = "my-org"
13+
14+
with InfluxDBClient(url=url, token=token) as client:
15+
buckets_api = client.buckets_api()
16+
17+
"""
18+
Create Bucket with retention policy set to 3600 seconds and name "bucket-by-python"
19+
"""
20+
print(f"------- Create -------\n")
21+
retention_rules = BucketRetentionRules(type="expire", every_seconds=3600)
22+
created_bucket = buckets_api.create_bucket(bucket_name="bucket-by-python",
23+
retention_rules=retention_rules,
24+
org=org)
25+
print(created_bucket)
26+
27+
"""
28+
List all Buckets
29+
"""
30+
print(f"\n------- List -------\n")
31+
buckets = buckets_api.find_buckets().buckets
32+
print("\n".join([f" ---\n ID: {bucket.id}\n Name: {bucket.name}\n Retention: {bucket.retention_rules}"
33+
for bucket in buckets]))
34+
print("---")
35+
36+
"""
37+
Delete previously created bucket
38+
"""
39+
print(f"------- Delete -------\n")
40+
buckets_api.delete_bucket(created_bucket)
41+
print(f" successfully deleted bucket: {created_bucket.name}")
2342

24-
write_api.write(bucket='my-bucket', record=record)

0 commit comments

Comments
 (0)