|
| 1 | +# Copyright 2019 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 | +# https://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 | + |
| 16 | +def create_fileset_entry_quickstart(client, project_id, entry_group_id, entry_id): |
| 17 | + |
| 18 | + # [START datacatalog_create_fileset_quickstart_tag] |
| 19 | + # Import required modules. |
| 20 | + from google.cloud import datacatalog_v1beta1 |
| 21 | + |
| 22 | + # TODO(developer): Construct a Data Catalog client object. |
| 23 | + # client = datacatalog_v1beta1.DataCatalogClient() |
| 24 | + |
| 25 | + # TODO(developer): Set project_id to your |
| 26 | + # Google Cloud Platform project ID the entry will belong. |
| 27 | + # project_id = "your-project-id" |
| 28 | + |
| 29 | + # TODO(developer): Specify the geographic location where the |
| 30 | + # entry should reside. |
| 31 | + # Currently, Data Catalog stores metadata in the us-central1 region. |
| 32 | + location_id = "us-central1" |
| 33 | + |
| 34 | + # TODO(developer): Set entry_group_id to the ID of the entry group |
| 35 | + # the entry will belong. |
| 36 | + # entry_group_id = "your_entry_group_id" |
| 37 | + |
| 38 | + # TODO(developer): Set entry_id to the ID of the entry to create. |
| 39 | + # entry_id = "your_entry_id" |
| 40 | + |
| 41 | + # Create an Entry Group. |
| 42 | + # Construct a full Entry Group object to send to the API. |
| 43 | + entry_group_obj = datacatalog_v1beta1.types.EntryGroup() |
| 44 | + entry_group_obj.display_name = "My Fileset Entry Group" |
| 45 | + entry_group_obj.description = "This Entry Group consists of ...." |
| 46 | + |
| 47 | + # Send the Entry Group to the API for creation. |
| 48 | + # Raises google.api_core.exceptions.AlreadyExists if the Entry Group |
| 49 | + # already exists within the project. |
| 50 | + entry_group = client.create_entry_group( |
| 51 | + parent=datacatalog_v1beta1.DataCatalogClient.location_path( |
| 52 | + project_id, location_id |
| 53 | + ), |
| 54 | + entry_group_id=entry_group_id, |
| 55 | + entry_group=entry_group_obj, |
| 56 | + ) |
| 57 | + print("Created entry group {}".format(entry_group.name)) |
| 58 | + |
| 59 | + # Create a Fileset Entry. |
| 60 | + # Construct a full Entry object to send to the API. |
| 61 | + entry = datacatalog_v1beta1.types.Entry() |
| 62 | + entry.display_name = "My Fileset" |
| 63 | + entry.description = "This Fileset consists of ..." |
| 64 | + entry.gcs_fileset_spec.file_patterns.append("gs://cloud-samples-data/*") |
| 65 | + entry.type = datacatalog_v1beta1.enums.EntryType.FILESET |
| 66 | + |
| 67 | + # Create the Schema, for example when you have a csv file. |
| 68 | + columns = [] |
| 69 | + columns.append( |
| 70 | + datacatalog_v1beta1.types.ColumnSchema( |
| 71 | + column="first_name", |
| 72 | + description="First name", |
| 73 | + mode="REQUIRED", |
| 74 | + type="STRING", |
| 75 | + ) |
| 76 | + ) |
| 77 | + |
| 78 | + columns.append( |
| 79 | + datacatalog_v1beta1.types.ColumnSchema( |
| 80 | + column="last_name", description="Last name", mode="REQUIRED", type="STRING" |
| 81 | + ) |
| 82 | + ) |
| 83 | + |
| 84 | + # Create sub columns for the addresses parent column |
| 85 | + subcolumns = [] |
| 86 | + subcolumns.append( |
| 87 | + datacatalog_v1beta1.types.ColumnSchema( |
| 88 | + column="city", description="City", mode="NULLABLE", type="STRING" |
| 89 | + ) |
| 90 | + ) |
| 91 | + |
| 92 | + subcolumns.append( |
| 93 | + datacatalog_v1beta1.types.ColumnSchema( |
| 94 | + column="state", description="State", mode="NULLABLE", type="STRING" |
| 95 | + ) |
| 96 | + ) |
| 97 | + |
| 98 | + columns.append( |
| 99 | + datacatalog_v1beta1.types.ColumnSchema( |
| 100 | + column="addresses", |
| 101 | + description="Addresses", |
| 102 | + mode="REPEATED", |
| 103 | + subcolumns=subcolumns, |
| 104 | + type="RECORD", |
| 105 | + ) |
| 106 | + ) |
| 107 | + |
| 108 | + entry.schema.columns.extend(columns) |
| 109 | + |
| 110 | + # Send the entry to the API for creation. |
| 111 | + # Raises google.api_core.exceptions.AlreadyExists if the Entry already |
| 112 | + # exists within the project. |
| 113 | + entry = client.create_entry(entry_group.name, entry_id, entry) |
| 114 | + print("Created entry {}".format(entry.name)) |
| 115 | + # [END datacatalog_create_fileset_quickstart_tag] |
0 commit comments