Skip to content

Commit 5e56938

Browse files
authored
docs: added an example how to use Buckets API (#213)
1 parent 0a78566 commit 5e56938

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
### Documentation
1313
1. [#202](https://github.com/influxdata/influxdb-client-python/pull/202): Added an example how to use RxPY and sync batching
14+
1. [#213](https://github.com/influxdata/influxdb-client-python/pull/213): Added an example how to use Buckets API
1415

1516
## 1.15.0 [2021-03-05]
1617

examples/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
- [query.py](query.py) - How to query data into `FluxTable`s, `Stream` and `CSV`
1212
- [query_from_file.py](query_from_file.py) - How to use a Flux query defined in a separate file
1313

14+
15+
## Management API
16+
- [buckets_management.py](buckets_management.py) - How to create, list and delete Buckets
17+
1418
## Others
1519
- [influx_cloud.py](influx_cloud.py) - How to connect to InfluxDB 2 Cloud
1620
- [influxdb_18_example.py](influxdb_18_example.py) - How to connect to InfluxDB 1.8

examples/buckets_management.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
How to How to create, list and delete Buckets.
3+
"""
4+
5+
from influxdb_client import InfluxDBClient, BucketRetentionRules
6+
7+
"""
8+
Define credentials
9+
"""
10+
url = "http://localhost:8086"
11+
token = "my-token"
12+
13+
client = InfluxDBClient(url=url, token=token)
14+
buckets_api = client.buckets_api()
15+
16+
"""
17+
The Bucket API uses as a parameter the Organization ID. We have to retrieve ID by Organization API.
18+
"""
19+
org_name = "my-org"
20+
org = list(filter(lambda it: it.name == org_name, client.organizations_api().find_organizations()))[0]
21+
22+
"""
23+
Create Bucket with retention policy set to 3600 seconds and name "bucket-by-python"
24+
"""
25+
print(f"------- Create -------\n")
26+
retention_rules = BucketRetentionRules(type="expire", every_seconds=3600)
27+
created_bucket = buckets_api.create_bucket(bucket_name="bucket-by-python",
28+
retention_rules=retention_rules,
29+
org_id=org.id)
30+
print(created_bucket)
31+
32+
"""
33+
List all Buckets
34+
"""
35+
print(f"\n------- List -------\n")
36+
buckets = buckets_api.find_buckets().buckets
37+
print("\n".join([f" ---\n ID: {bucket.id}\n Name: {bucket.name}\n Retention: {bucket.retention_rules}"
38+
for bucket in buckets]))
39+
print("---")
40+
41+
"""
42+
Delete previously created bucket
43+
"""
44+
print(f"------- Delete -------\n")
45+
buckets_api.delete_bucket(created_bucket)
46+
print(f" successfully deleted bucket: {created_bucket.name}")
47+
48+
client.close()

0 commit comments

Comments
 (0)