|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START dataplex_create_aspect_type] |
| 16 | +from typing import List |
| 17 | + |
| 18 | +from google.cloud import dataplex_v1 |
| 19 | + |
| 20 | + |
| 21 | +# Method to create Aspect Type located in project_id, location and with aspect_type_id and |
| 22 | +# aspect_fields specifying schema of the Aspect Type |
| 23 | +def create_aspect_type( |
| 24 | + project_id: str, |
| 25 | + location: str, |
| 26 | + aspect_type_id: str, |
| 27 | + aspect_fields: List[dataplex_v1.AspectType.MetadataTemplate], |
| 28 | +) -> dataplex_v1.AspectType: |
| 29 | + # Initialize client that will be used to send requests across threads. This |
| 30 | + # client only needs to be created once, and can be reused for multiple requests. |
| 31 | + # After completing all of your requests, call the "__exit__()" method to safely |
| 32 | + # clean up any remaining background resources. Alternatively, use the client as |
| 33 | + # a context manager. |
| 34 | + with dataplex_v1.CatalogServiceClient() as client: |
| 35 | + # The resource name of the Aspect Type location |
| 36 | + parent = f"projects/{project_id}/locations/{location}" |
| 37 | + aspect_type = dataplex_v1.AspectType( |
| 38 | + description="description of the aspect type", |
| 39 | + metadata_template=dataplex_v1.AspectType.MetadataTemplate( |
| 40 | + # The name must follow regex ^(([a-zA-Z]{1})([\\w\\-_]{0,62}))$ |
| 41 | + # That means name must only contain alphanumeric character or dashes or underscores, |
| 42 | + # start with an alphabet, and must be less than 63 characters. |
| 43 | + name="name_of_the_template", |
| 44 | + type="record", |
| 45 | + # Aspect Type fields, that themselves are Metadata Templates. |
| 46 | + record_fields=aspect_fields, |
| 47 | + ), |
| 48 | + ) |
| 49 | + create_operation = client.create_aspect_type( |
| 50 | + parent=parent, aspect_type=aspect_type, aspect_type_id=aspect_type_id |
| 51 | + ) |
| 52 | + return create_operation.result(60) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + # TODO(developer): Replace these variables before running the sample. |
| 57 | + project_id = "MY_PROJECT_ID" |
| 58 | + # Available locations: https://cloud.google.com/dataplex/docs/locations |
| 59 | + location = "MY_LOCATION" |
| 60 | + aspect_type_id = "MY_ASPECT_TYPE_ID" |
| 61 | + aspect_field = dataplex_v1.AspectType.MetadataTemplate( |
| 62 | + # The name must follow regex ^(([a-zA-Z]{1})([\\w\\-_]{0,62}))$ |
| 63 | + # That means name must only contain alphanumeric character or dashes or underscores, |
| 64 | + # start with an alphabet, and must be less than 63 characters. |
| 65 | + name="name_of_the_field", |
| 66 | + # Metadata Template is recursive structure, |
| 67 | + # primitive types such as "string" or "integer" indicate leaf node, |
| 68 | + # complex types such as "record" or "array" would require nested Metadata Template |
| 69 | + type="string", |
| 70 | + index=1, |
| 71 | + annotations=dataplex_v1.AspectType.MetadataTemplate.Annotations( |
| 72 | + description="description of the field" |
| 73 | + ), |
| 74 | + constraints=dataplex_v1.AspectType.MetadataTemplate.Constraints( |
| 75 | + # Specifies if field will be required in Aspect Type. |
| 76 | + required=True |
| 77 | + ), |
| 78 | + ) |
| 79 | + aspect_fields = [aspect_field] |
| 80 | + |
| 81 | + created_aspect_type = create_aspect_type( |
| 82 | + project_id, location, aspect_type_id, aspect_fields |
| 83 | + ) |
| 84 | + print(f"Successfully created aspect type: {created_aspect_type.name}") |
| 85 | +# [END dataplex_create_aspect_type] |
0 commit comments