Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit 16eaf4b

Browse files
mesmacostatswast
authored andcommitted
feat(datacatalog): add sample for create a fileset entry quickstart (#9977)
1 parent 9f8c58d commit 16eaf4b

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

samples/quickstart/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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]

samples/tests/conftest.py

+13
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ def project_id(default_credentials):
4242
return default_credentials[1]
4343

4444

45+
@pytest.fixture
46+
def random_entry_id(client, project_id, random_entry_group_id):
47+
now = datetime.datetime.now()
48+
random_entry_id = "example_entry_{}_{}".format(
49+
now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
50+
)
51+
yield random_entry_id
52+
entry_name = datacatalog_v1beta1.DataCatalogClient.entry_path(
53+
project_id, "us-central1", random_entry_group_id, random_entry_id
54+
)
55+
client.delete_entry(entry_name)
56+
57+
4558
@pytest.fixture
4659
def random_entry_group_id(client, project_id):
4760
now = datetime.datetime.now()

samples/tests/quickstart/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
from google.cloud import datacatalog_v1beta1
16+
17+
from ...quickstart import create_fileset_entry_quickstart
18+
19+
20+
def test_create_fileset_entry_quickstart(
21+
capsys, client, project_id, random_entry_group_id, random_entry_id
22+
):
23+
24+
create_fileset_entry_quickstart.create_fileset_entry_quickstart(
25+
client, project_id, random_entry_group_id, random_entry_id
26+
)
27+
out, err = capsys.readouterr()
28+
assert (
29+
"Created entry group"
30+
" projects/{}/locations/{}/entryGroups/{}".format(
31+
project_id, "us-central1", random_entry_group_id
32+
)
33+
in out
34+
)
35+
36+
expected_entry_name = datacatalog_v1beta1.DataCatalogClient.entry_path(
37+
project_id, "us-central1", random_entry_group_id, random_entry_id
38+
)
39+
40+
assert "Created entry {}".format(expected_entry_name) in out

0 commit comments

Comments
 (0)