-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Add samples for Data Catalog lookup_entry #2148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tswast
merged 14 commits into
GoogleCloudPlatform:master
from
ricardolsmendes:task/add-lookup-entry-samples
May 10, 2019
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2550064
Add samples for Data Catalog lookup_entry
3fcdb2b
Add tests for Data Catalog lookup_entry
fa90bbf
Add samples for lookup_entry by SQL Resource
e30befb
Add README.rst
9ab7b1d
Fix README.rst
061c18a
Update comments to improve README.rst formatting
d843626
Update comments to improve README.rst formatting
4fc7287
Fix max line length issues
685fcbb
Fix max line length issues
df86495
Pin google-cloud-datacatalog version number
fb77f4d
Readability improvements
531cbf0
Remove @eventually_consistent decorator
d42fbf2
Improve command line interface
10450b2
Refer to GCP public assets in tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| .. This file is automatically generated. Do not edit this file directly. | ||
|
|
||
| Google Cloud Data Catalog Python Samples | ||
| =============================================================================== | ||
|
|
||
| .. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
| :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=datacatalog/cloud-client/README.rst | ||
|
|
||
|
|
||
| This directory contains samples for Google Cloud Data Catalog. `Google Cloud Data Catalog`_ is a fully managed and scalable metadata management service that empowers organizations to quickly discover, manage, and understand all their data in Google Cloud. | ||
|
|
||
|
|
||
|
|
||
|
|
||
| .. _Google Cloud Data Catalog: https://cloud.google.com/data-catalog/docs | ||
|
|
||
| Setup | ||
| ------------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| Authentication | ||
| ++++++++++++++ | ||
|
|
||
| This sample requires you to have authentication setup. Refer to the | ||
| `Authentication Getting Started Guide`_ for instructions on setting up | ||
| credentials for applications. | ||
|
|
||
| .. _Authentication Getting Started Guide: | ||
| https://cloud.google.com/docs/authentication/getting-started | ||
|
|
||
| Install Dependencies | ||
| ++++++++++++++++++++ | ||
|
|
||
| #. Clone python-docs-samples and change directory to the sample directory you want to use. | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git | ||
|
|
||
| #. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions. | ||
|
|
||
| .. _Python Development Environment Setup Guide: | ||
| https://cloud.google.com/python/setup | ||
|
|
||
| #. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| $ virtualenv env | ||
| $ source env/bin/activate | ||
|
|
||
| #. Install the dependencies needed to run the samples. | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| $ pip install -r requirements.txt | ||
|
|
||
| .. _pip: https://pip.pypa.io/ | ||
| .. _virtualenv: https://virtualenv.pypa.io/ | ||
|
|
||
| Samples | ||
| ------------------------------------------------------------------------------- | ||
|
|
||
| Lookup entry | ||
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
|
||
| .. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
| :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=datacatalog/cloud-client/lookup_entry.py,datacatalog/cloud-client/README.rst | ||
|
|
||
|
|
||
|
|
||
|
|
||
| To run this sample: | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| $ python lookup_entry.py | ||
|
|
||
| usage: lookup_entry.py [-h] [--sql-resource] | ||
| project_id | ||
| {lookup-bigquery-dataset,lookup-bigquery-table,lookup-pubsub-topic} | ||
| ... | ||
|
|
||
| This application demonstrates how to perform basic operations on entries | ||
| with the Cloud Data Catalog API. | ||
|
|
||
| For more information, see the README.md under /datacatalog and the | ||
| documentation at https://cloud.google.com/data-catalog/docs. | ||
|
|
||
| positional arguments: | ||
| project_id Your Google Cloud project ID | ||
| {lookup-bigquery-dataset,lookup-bigquery-table,lookup-pubsub-topic} | ||
| lookup-bigquery-dataset | ||
| Retrieves Data Catalog entry for the given BigQuery | ||
| Dataset. | ||
| lookup-bigquery-table | ||
| Retrieves Data Catalog entry for the given BigQuery | ||
| Table. | ||
| lookup-pubsub-topic | ||
| Retrieves Data Catalog entry for the given Pub/Sub | ||
| Topic. | ||
|
|
||
| optional arguments: | ||
| -h, --help show this help message and exit | ||
| --sql-resource Perform lookup by SQL Resource | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| The client library | ||
| ------------------------------------------------------------------------------- | ||
|
|
||
| This sample uses the `Google Cloud Client Library for Python`_. | ||
| You can read the documentation for more details on API usage and use GitHub | ||
| to `browse the source`_ and `report issues`_. | ||
|
|
||
| .. _Google Cloud Client Library for Python: | ||
| https://googlecloudplatform.github.io/google-cloud-python/ | ||
| .. _browse the source: | ||
| https://github.com/GoogleCloudPlatform/google-cloud-python | ||
| .. _report issues: | ||
| https://github.com/GoogleCloudPlatform/google-cloud-python/issues | ||
|
|
||
|
|
||
| .. _Google Cloud SDK: https://cloud.google.com/sdk/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # This file is used to generate README.rst | ||
|
|
||
| product: | ||
| name: Google Cloud Data Catalog | ||
| short_name: Data Catalog | ||
| url: https://cloud.google.com/data-catalog/docs | ||
| description: > | ||
| `Google Cloud Data Catalog`_ is a fully managed and scalable metadata | ||
| management service that empowers organizations to quickly discover, manage, | ||
| and understand all their data in Google Cloud. | ||
|
|
||
| setup: | ||
| - auth | ||
| - install_deps | ||
|
|
||
| samples: | ||
| - name: Lookup entry | ||
| file: lookup_entry.py | ||
| show_help: true | ||
|
|
||
| cloud_client_library: true | ||
|
|
||
| folder: datacatalog/cloud-client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2019 Google Inc. All Rights Reserved. | ||
| # | ||
| # 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| """This application demonstrates how to perform basic operations on entries | ||
| with the Cloud Data Catalog API. | ||
|
|
||
| For more information, see the README.md under /datacatalog and the | ||
| documentation at https://cloud.google.com/data-catalog/docs. | ||
| """ | ||
|
|
||
| import argparse | ||
|
|
||
|
|
||
| def lookup_bigquery_dataset(project_id, dataset_id): | ||
| """Retrieves Data Catalog entry for the given BigQuery Dataset.""" | ||
| from google.cloud import datacatalog_v1beta1 | ||
|
|
||
| datacatalog = datacatalog_v1beta1.DataCatalogClient() | ||
|
|
||
| resource_name = '//bigquery.googleapis.com/projects/{}/datasets/{}'\ | ||
| .format(project_id, dataset_id) | ||
|
|
||
| return datacatalog.lookup_entry(linked_resource=resource_name) | ||
|
|
||
|
|
||
| def lookup_bigquery_dataset_sql_resource(project_id, dataset_id): | ||
| """Retrieves Data Catalog entry for the given BigQuery Dataset by | ||
| sql_resource. | ||
| """ | ||
| from google.cloud import datacatalog_v1beta1 | ||
|
|
||
| datacatalog = datacatalog_v1beta1.DataCatalogClient() | ||
|
|
||
| sql_resource = 'bigquery.dataset.`{}`.`{}`'.format(project_id, dataset_id) | ||
|
|
||
| return datacatalog.lookup_entry(sql_resource=sql_resource) | ||
|
|
||
|
|
||
| def lookup_bigquery_table(project_id, dataset_id, table_id): | ||
| """Retrieves Data Catalog entry for the given BigQuery Table.""" | ||
| from google.cloud import datacatalog_v1beta1 | ||
|
|
||
| datacatalog = datacatalog_v1beta1.DataCatalogClient() | ||
|
|
||
| resource_name = '//bigquery.googleapis.com/projects/{}/datasets/{}' \ | ||
| '/tables/{}'\ | ||
| .format(project_id, dataset_id, table_id) | ||
|
|
||
| return datacatalog.lookup_entry(linked_resource=resource_name) | ||
|
|
||
|
|
||
| def lookup_bigquery_table_sql_resource(project_id, dataset_id, table_id): | ||
| """Retrieves Data Catalog entry for the given BigQuery Table by | ||
| sql_resource. | ||
| """ | ||
| from google.cloud import datacatalog_v1beta1 | ||
|
|
||
| datacatalog = datacatalog_v1beta1.DataCatalogClient() | ||
|
|
||
| sql_resource = 'bigquery.table.`{}`.`{}`.`{}`'.format( | ||
| project_id, dataset_id, table_id) | ||
|
|
||
| return datacatalog.lookup_entry(sql_resource=sql_resource) | ||
|
|
||
|
|
||
| def lookup_pubsub_topic(project_id, topic_id): | ||
| """Retrieves Data Catalog entry for the given Pub/Sub Topic.""" | ||
| from google.cloud import datacatalog_v1beta1 | ||
|
|
||
| datacatalog = datacatalog_v1beta1.DataCatalogClient() | ||
|
|
||
| resource_name = '//pubsub.googleapis.com/projects/{}/topics/{}'\ | ||
| .format(project_id, topic_id) | ||
|
|
||
| return datacatalog.lookup_entry(linked_resource=resource_name) | ||
|
|
||
|
|
||
| def lookup_pubsub_topic_sql_resource(project_id, topic_id): | ||
| """Retrieves Data Catalog entry for the given Pub/Sub Topic by | ||
| sql_resource. | ||
| """ | ||
| from google.cloud import datacatalog_v1beta1 | ||
|
|
||
| datacatalog = datacatalog_v1beta1.DataCatalogClient() | ||
|
|
||
| sql_resource = 'pubsub.topic.`{}`.`{}`'.format(project_id, topic_id) | ||
|
|
||
| return datacatalog.lookup_entry(sql_resource=sql_resource) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| parser = argparse.ArgumentParser( | ||
| description=__doc__, | ||
| formatter_class=argparse.RawDescriptionHelpFormatter | ||
| ) | ||
| parser.add_argument('project_id', help='Your Google Cloud project ID') | ||
| parser.add_argument('--sql-resource', action='store_true', | ||
| help='Perform lookup by SQL Resource') | ||
|
|
||
| subparsers = parser.add_subparsers(dest='command') | ||
|
|
||
| bigquery_dataset_parser = subparsers.add_parser( | ||
| 'lookup-bigquery-dataset', help=lookup_bigquery_dataset.__doc__) | ||
| bigquery_dataset_parser.add_argument('dataset_id') | ||
|
|
||
| bigquery_table_parser = subparsers.add_parser( | ||
| 'lookup-bigquery-table', help=lookup_bigquery_table.__doc__) | ||
| bigquery_table_parser.add_argument('dataset_id') | ||
| bigquery_table_parser.add_argument('table_id') | ||
|
|
||
| pubsub_topic_parser = subparsers.add_parser( | ||
| 'lookup-pubsub-topic', help=lookup_pubsub_topic.__doc__) | ||
| pubsub_topic_parser.add_argument('topic_id') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| entry = None | ||
|
|
||
| if args.command == 'lookup-bigquery-dataset': | ||
| lookup_method = lookup_bigquery_dataset_sql_resource \ | ||
| if args.sql_resource else lookup_bigquery_dataset | ||
| entry = lookup_method(args.project_id, args.dataset_id) | ||
| elif args.command == 'lookup-bigquery-table': | ||
| lookup_method = lookup_bigquery_table_sql_resource \ | ||
| if args.sql_resource else lookup_bigquery_table | ||
| entry = lookup_method(args.project_id, args.dataset_id, args.table_id) | ||
| elif args.command == 'lookup-pubsub-topic': | ||
| lookup_method = lookup_pubsub_topic_sql_resource \ | ||
| if args.sql_resource else lookup_pubsub_topic | ||
| entry = lookup_method(args.project_id, args.topic_id) | ||
|
|
||
| print(entry.name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2019 Google Inc. All Rights Reserved. | ||
| # | ||
| # 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| import os | ||
|
|
||
| from gcp_devrel.testing import eventually_consistent | ||
|
|
||
| import lookup_entry | ||
|
|
||
| PROJECT = os.environ['GCLOUD_PROJECT'] | ||
| BIGQUERY_DATASET = os.environ['GCLOUD_BIGQUERY_DATASET'] | ||
| BIGQUERY_TABLE = os.environ['GCLOUD_BIGQUERY_TABLE'] | ||
| PUBSUB_TOPIC = os.environ['GCLOUD_PUBSUB_TOPIC'] | ||
|
|
||
|
|
||
| def test_lookup_bigquery_dataset(): | ||
| @eventually_consistent.call | ||
This conversation was marked as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def _(): | ||
| assert lookup_entry.lookup_bigquery_dataset(PROJECT, BIGQUERY_DATASET) | ||
|
|
||
|
|
||
| def test_lookup_bigquery_dataset_sql_resource(): | ||
| @eventually_consistent.call | ||
| def _(): | ||
| assert lookup_entry.lookup_bigquery_dataset_sql_resource( | ||
| PROJECT, BIGQUERY_DATASET) | ||
|
|
||
|
|
||
| def test_lookup_bigquery_table(): | ||
| @eventually_consistent.call | ||
| def _(): | ||
| assert lookup_entry.lookup_bigquery_table( | ||
| PROJECT, BIGQUERY_DATASET, BIGQUERY_TABLE) | ||
|
|
||
|
|
||
| def test_lookup_bigquery_table_sql_resource(): | ||
| @eventually_consistent.call | ||
| def _(): | ||
| assert lookup_entry.lookup_bigquery_table_sql_resource( | ||
| PROJECT, BIGQUERY_DATASET, BIGQUERY_TABLE) | ||
|
|
||
|
|
||
| def test_lookup_pubsub_topic(): | ||
| @eventually_consistent.call | ||
| def _(): | ||
| assert lookup_entry.lookup_pubsub_topic(PROJECT, PUBSUB_TOPIC) | ||
|
|
||
|
|
||
| def test_lookup_pubsub_topic_sql_resource(): | ||
| @eventually_consistent.call | ||
| def _(): | ||
| assert lookup_entry.lookup_pubsub_topic_sql_resource( | ||
| PROJECT, PUBSUB_TOPIC) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| google-cloud-datacatalog==0.1.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.