|
| 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(client, entry_group_name, entry_id): |
| 17 | + |
| 18 | + # [START datacatalog_create_fileset_tag] |
| 19 | + from google.cloud import datacatalog_v1beta1 |
| 20 | + |
| 21 | + # TODO(developer): Construct a Data Catalog client object. |
| 22 | + # client = datacatalog_v1beta1.DataCatalogClient() |
| 23 | + |
| 24 | + # TODO(developer): Set entry_group_name to the Name of the entry group |
| 25 | + # the entry will belong. |
| 26 | + # entry_group_name = "your_entry_group_name" |
| 27 | + |
| 28 | + # TODO(developer): Set entry_id to the ID of the entry to create. |
| 29 | + # entry_id = "your_entry_id" |
| 30 | + |
| 31 | + # Construct a full Entry object to send to the API. |
| 32 | + entry = datacatalog_v1beta1.types.Entry() |
| 33 | + entry.display_name = "My Fileset" |
| 34 | + entry.description = "This Fileset consists of ..." |
| 35 | + entry.gcs_fileset_spec.file_patterns.append("gs://my_bucket/*") |
| 36 | + entry.type = datacatalog_v1beta1.enums.EntryType.FILESET |
| 37 | + |
| 38 | + # Create the Schema, for example when you have a csv file. |
| 39 | + columns = [] |
| 40 | + columns.append( |
| 41 | + datacatalog_v1beta1.types.ColumnSchema( |
| 42 | + column="first_name", |
| 43 | + description="First name", |
| 44 | + mode="REQUIRED", |
| 45 | + type="STRING", |
| 46 | + ) |
| 47 | + ) |
| 48 | + |
| 49 | + columns.append( |
| 50 | + datacatalog_v1beta1.types.ColumnSchema( |
| 51 | + column="last_name", description="Last name", mode="REQUIRED", type="STRING" |
| 52 | + ) |
| 53 | + ) |
| 54 | + |
| 55 | + # Create sub columns for the addresses parent column |
| 56 | + subcolumns = [] |
| 57 | + subcolumns.append( |
| 58 | + datacatalog_v1beta1.types.ColumnSchema( |
| 59 | + column="city", description="City", mode="NULLABLE", type="STRING" |
| 60 | + ) |
| 61 | + ) |
| 62 | + |
| 63 | + subcolumns.append( |
| 64 | + datacatalog_v1beta1.types.ColumnSchema( |
| 65 | + column="state", description="State", mode="NULLABLE", type="STRING" |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + columns.append( |
| 70 | + datacatalog_v1beta1.types.ColumnSchema( |
| 71 | + column="addresses", |
| 72 | + description="Addresses", |
| 73 | + mode="REPEATED", |
| 74 | + subcolumns=subcolumns, |
| 75 | + type="RECORD", |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + entry.schema.columns.extend(columns) |
| 80 | + |
| 81 | + # Send the entry to the API for creation. |
| 82 | + # Raises google.api_core.exceptions.AlreadyExists if the Entry already |
| 83 | + # exists within the project. |
| 84 | + entry = client.create_entry(entry_group_name, entry_id, entry) |
| 85 | + print("Created entry {}".format(entry.name)) |
| 86 | + # [END datacatalog_create_fileset_tag] |
0 commit comments