Skip to content

It's unclear from the doc how to manage the buckets #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andkuzmich opened this issue Mar 15, 2021 · 4 comments · Fixed by #213
Closed

It's unclear from the doc how to manage the buckets #210

andkuzmich opened this issue Mar 15, 2021 · 4 comments · Fixed by #213
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@andkuzmich
Copy link

It would be nice if you will provide a fancy example for buckets management API. It's not well documented and requires to go into code if you want to figure out how to create/list/delete the buckets. A simple use case is 'CREATE IF NOT EXISTS' on fly while producer submitting a datahttps://guides.github.com/features/mastering-markdown/

@bednar
Copy link
Contributor

bednar commented Mar 16, 2021

@andkuzmich, thanks for using our client, We will take a look.

@bednar bednar added the documentation Improvements or additions to documentation label Mar 16, 2021
@bednar
Copy link
Contributor

bednar commented Mar 16, 2021

There is a doc for BucketsApi: https://influxdb-client.readthedocs.io/en/latest/api.html#bucketsapi

simple use case is 'CREATE IF NOT EXISTS' on fly while producer submitting a data

This is not simple use case because it suppose that you have a permission to create a bucket.

You could use something like this to create bucket if not exists:

from influxdb_client import InfluxDBClient, Point, BucketRetentionRules
from influxdb_client.client.write_api import SYNCHRONOUS
from influxdb_client.rest import ApiException

org_id = "36e4880c1d86284a"
bucket = "my-bucket_to_create"
token = "my-token"

client = InfluxDBClient(url="http://localhost:8086", token=token, org=org_id)
write_api = client.write_api(write_options=SYNCHRONOUS)
query_api = client.query_api()


def create_bucket_if_need():
    try:
        write_api.write(bucket, org_id, b'')
    except ApiException as e:
        if e.status == 404:
            buckets_api = client.buckets_api()
            retention_rules = BucketRetentionRules(type='expire', every_seconds=3600)
            created_bucket = buckets_api.create_bucket(bucket_name=bucket,
                                                       retention_rules=retention_rules,
                                                       org_id=org_id)
            print(f"created bucket: {created_bucket}")
        pass


create_bucket_if_need()

"""
Write Data
"""
point = Point("mem").tag("host", "host1").field("used_percent", 25.43234543)
write_api.write(bucket=bucket, record=point)

"""
Query Data
"""
tables = query_api.query(f'from(bucket: "{bucket}") |> range(start: 0)')
print('\n'.join([f"{record.values}" for records in tables for record in records]))

client.close()

@bednar bednar added this to the 1.16.0 milestone Mar 19, 2021
@bednar
Copy link
Contributor

bednar commented Mar 19, 2021

@andkuzmich, check it out https://github.com/influxdata/influxdb-client-python/blob/master/examples/buckets_management.py

@andkuzmich
Copy link
Author

andkuzmich commented Mar 19, 2021

@bednar, Thank you a lot! I've figured it out from the code, but anyway your example might be helpful for others

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants