|
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 | +""" |
3 | 4 |
|
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 |
6 | 6 |
|
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}") |
23 | 42 |
|
24 |
| - write_api.write(bucket='my-bucket', record=record) |
|
0 commit comments