Skip to content

Commit f72ab40

Browse files
ricardolsmendestswast
authored andcommitted
feat(datacatalog): add sample to create a fileset entry (#9590)
Fixes #9589
1 parent ed37540 commit f72ab40

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

datacatalog/samples/tests/conftest.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,29 @@ def random_entry_group_id(client, project_id):
5353
project_id, "us-central1", random_entry_group_id
5454
)
5555
client.delete_entry_group(entry_group_name)
56+
57+
58+
@pytest.fixture
59+
def random_entry_name(client, entry_group_name):
60+
now = datetime.datetime.now()
61+
random_entry_id = "example_entry_{}_{}".format(
62+
now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
63+
)
64+
random_entry_name = "{}/entries/{}".format(entry_group_name, random_entry_id)
65+
yield random_entry_name
66+
client.delete_entry(random_entry_name)
67+
68+
69+
@pytest.fixture
70+
def entry_group_name(client, project_id):
71+
now = datetime.datetime.now()
72+
entry_group_id = "python_entry_group_sample_{}_{}".format(
73+
now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
74+
)
75+
entry_group = client.create_entry_group(
76+
datacatalog_v1beta1.DataCatalogClient.location_path(project_id, "us-central1"),
77+
entry_group_id,
78+
{},
79+
)
80+
yield entry_group.name
81+
client.delete_entry_group(entry_group.name)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
import re
17+
18+
from ..v1beta1 import create_fileset_entry
19+
20+
21+
def test_create_fileset_entry(capsys, client, random_entry_name):
22+
23+
entry_name_pattern = "(?P<entry_group_name>.+?)/entries/(?P<entry_id>.+?$)"
24+
entry_name_matches = re.match(entry_name_pattern, random_entry_name)
25+
entry_group_name = entry_name_matches.group("entry_group_name")
26+
entry_id = entry_name_matches.group("entry_id")
27+
28+
create_fileset_entry.create_fileset_entry(client, entry_group_name, entry_id)
29+
out, err = capsys.readouterr()
30+
assert "Created entry {}".format(random_entry_name) in out
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)