|
| 1 | +""" |
| 2 | +This example is related to `InfluxDB Cloud <https://docs.influxdata.com/influxdb/cloud/>`_ and not available |
| 3 | +on a local InfluxDB OSS instance. |
| 4 | +
|
| 5 | +How to manage explicit bucket schemas to enforce column names, tags, fields, and data types for your data. |
| 6 | +""" |
| 7 | +import datetime |
| 8 | + |
| 9 | +from influxdb_client import InfluxDBClient, BucketSchemasService, PostBucketRequest, SchemaType, \ |
| 10 | + MeasurementSchemaCreateRequest, MeasurementSchemaColumn, ColumnSemanticType, ColumnDataType, \ |
| 11 | + MeasurementSchemaUpdateRequest |
| 12 | + |
| 13 | +""" |
| 14 | +Define credentials |
| 15 | +""" |
| 16 | +influx_cloud_url = 'https://us-west-2-1.aws.cloud2.influxdata.com' |
| 17 | +influx_cloud_token = '...' |
| 18 | +org_name = '...' |
| 19 | + |
| 20 | +with InfluxDBClient(url=influx_cloud_url, token=influx_cloud_token, org=org_name, debug=False) as client: |
| 21 | + uniqueId = str(datetime.datetime.now()) |
| 22 | + org_id = client.organizations_api().find_organizations(org=org_name)[0].id |
| 23 | + bucket_schemas_api = BucketSchemasService(api_client=client.api_client) |
| 24 | + |
| 25 | + """ |
| 26 | + Create a bucket with the schema_type flag set to explicit |
| 27 | + """ |
| 28 | + print("------- Create Bucket -------\n") |
| 29 | + created_bucket = client \ |
| 30 | + .buckets_api() \ |
| 31 | + .create_bucket(bucket=PostBucketRequest(name=f"my_schema_bucket_{uniqueId}", |
| 32 | + org_id=org_id, |
| 33 | + retention_rules=[], |
| 34 | + schema_type=SchemaType.EXPLICIT)) |
| 35 | + print(created_bucket) |
| 36 | + |
| 37 | + """ |
| 38 | + Sets the schema for a measurement: Usage CPU |
| 39 | +
|
| 40 | + [ |
| 41 | + {"name": "time", "type": "timestamp"}, |
| 42 | + {"name": "service", "type": "tag"}, |
| 43 | + {"name": "host", "type": "tag"}, |
| 44 | + {"name": "usage_user", "type": "field", "dataType": "float"}, |
| 45 | + {"name": "usage_system", "type": "field", "dataType": "float"} |
| 46 | + ] |
| 47 | + """ |
| 48 | + print("------- Create Schema -------\n") |
| 49 | + columns = [ |
| 50 | + MeasurementSchemaColumn(name="time", |
| 51 | + type=ColumnSemanticType.TIMESTAMP), |
| 52 | + MeasurementSchemaColumn(name="service", |
| 53 | + type=ColumnSemanticType.TAG), |
| 54 | + MeasurementSchemaColumn(name="host", |
| 55 | + type=ColumnSemanticType.TAG), |
| 56 | + MeasurementSchemaColumn(name="usage_user", |
| 57 | + type=ColumnSemanticType.FIELD, |
| 58 | + data_type=ColumnDataType.FLOAT), |
| 59 | + MeasurementSchemaColumn(name="usage_system", |
| 60 | + type=ColumnSemanticType.FIELD, |
| 61 | + data_type=ColumnDataType.FLOAT) |
| 62 | + ] |
| 63 | + create_request = MeasurementSchemaCreateRequest(name="usage_cpu", columns=columns) |
| 64 | + created_schema = bucket_schemas_api.create_measurement_schema(bucket_id=created_bucket.id, |
| 65 | + org_id=org_id, |
| 66 | + measurement_schema_create_request=create_request) |
| 67 | + print(created_bucket) |
| 68 | + |
| 69 | + """ |
| 70 | + Lists the Schemas |
| 71 | + """ |
| 72 | + print("\n------- Lists the Schemas -------\n") |
| 73 | + measurement_schemas = bucket_schemas_api.get_measurement_schemas(bucket_id=created_bucket.id).measurement_schemas |
| 74 | + print("\n".join([f"---\n ID: {ms.id}\n Name: {ms.name}\n Columns: {ms.columns}" for ms in measurement_schemas])) |
| 75 | + print("---") |
| 76 | + |
| 77 | + """ |
| 78 | + Update a bucket schema |
| 79 | + """ |
| 80 | + print("------- Update a bucket schema -------\n") |
| 81 | + columns.append(MeasurementSchemaColumn(name="usage_total", |
| 82 | + type=ColumnSemanticType.FIELD, |
| 83 | + data_type=ColumnDataType.FLOAT)) |
| 84 | + update_request = MeasurementSchemaUpdateRequest(columns=columns) |
| 85 | + updated_schema = bucket_schemas_api.update_measurement_schema(bucket_id=created_bucket.id, |
| 86 | + measurement_id=created_schema.id, |
| 87 | + measurement_schema_update_request=update_request) |
| 88 | + print(updated_schema) |
| 89 | + |
| 90 | + """ |
| 91 | + Delete previously created bucket |
| 92 | + """ |
| 93 | + print("------- Delete Bucket -------\n") |
| 94 | + client.buckets_api().delete_bucket(created_bucket) |
| 95 | + print(f" successfully deleted bucket: {created_bucket.name}") |
0 commit comments