-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathlookup_entry.py
More file actions
96 lines (77 loc) · 3.57 KB
/
Copy pathlookup_entry.py
File metadata and controls
96 lines (77 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
def lookup_entry(override_values):
"""Retrieves Data Catalog entry for the given Google Cloud Platform resource."""
# [START data_catalog_lookup_dataset]
# [START data_catalog_lookup_entry]
from google.cloud import datacatalog_v1
datacatalog = datacatalog_v1.DataCatalogClient()
bigquery_project_id = "my_bigquery_project"
dataset_id = "my_dataset"
table_id = "my_table"
pubsub_project_id = "my_pubsub_project"
topic_id = "my_topic"
# [END data_catalog_lookup_entry]
# To facilitate testing, we replace values with alternatives
# provided by the testing harness.
bigquery_project_id = override_values.get(
"bigquery_project_id", bigquery_project_id
)
dataset_id = override_values.get("dataset_id", dataset_id)
table_id = override_values.get("table_id", table_id)
pubsub_project_id = override_values.get("pubsub_project_id", pubsub_project_id)
topic_id = override_values.get("topic_id", topic_id)
# [START data_catalog_lookup_entry]
# BigQuery Dataset via linked_resource
resource_name = f"//bigquery.googleapis.com/projects/{bigquery_project_id}/datasets/{dataset_id}"
entry = datacatalog.lookup_entry(request={"linked_resource": resource_name})
print(
f"Retrieved entry {entry.name} for BigQuery Dataset resource {entry.linked_resource}"
)
# BigQuery Dataset via sql_resource
sql_resource = f"bigquery.dataset.`{bigquery_project_id}`.`{dataset_id}`"
entry = datacatalog.lookup_entry(request={"sql_resource": sql_resource})
print(
f"Retrieved entry {entry.name} for BigQuery Dataset resource {entry.linked_resource}"
)
# BigQuery Table via linked_resource
resource_name = (
f"//bigquery.googleapis.com/projects/{bigquery_project_id}/datasets/{dataset_id}"
f"/tables/{table_id}"
)
entry = datacatalog.lookup_entry(request={"linked_resource": resource_name})
print(f"Retrieved entry {entry.name} for BigQuery Table {entry.linked_resource}")
# BigQuery Table via sql_resource
sql_resource = f"bigquery.table.`{bigquery_project_id}`.`{dataset_id}`.`{table_id}`"
entry = datacatalog.lookup_entry(request={"sql_resource": sql_resource})
print(
f"Retrieved entry {entry.name} for BigQuery Table resource {entry.linked_resource}"
)
# Pub/Sub Topic via linked_resource
resource_name = (
f"//pubsub.googleapis.com/projects/{pubsub_project_id}/topics/{topic_id}"
)
entry = datacatalog.lookup_entry(request={"linked_resource": resource_name})
print(
f"Retrieved entry {entry.name} for Pub/Sub Topic resource {entry.linked_resource}"
)
# Pub/Sub Topic via sql_resource
sql_resource = f"pubsub.topic.`{pubsub_project_id}`.`{topic_id}`"
entry = datacatalog.lookup_entry(request={"sql_resource": sql_resource})
print(
f"Retrieved entry {entry.name} for Pub/Sub Topic resource {entry.linked_resource}"
)
# [END data_catalog_lookup_entry]
# [END data_catalog_lookup_dataset]