Skip to content
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

Add samples for Data Catalog lookup_entry #2148

Merged
126 changes: 126 additions & 0 deletions datacatalog/cloud-client/README.rst
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/
23 changes: 23 additions & 0 deletions datacatalog/cloud-client/README.rst.in
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
160 changes: 160 additions & 0 deletions datacatalog/cloud-client/lookup_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/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)

entry = datacatalog.lookup_entry(linked_resource=resource_name)
print(entry.name)
return entry


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)

entry = datacatalog.lookup_entry(sql_resource=sql_resource)
print(entry.name)
return entry


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)

entry = datacatalog.lookup_entry(linked_resource=resource_name)
print(entry.name)
return entry


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)

entry = datacatalog.lookup_entry(sql_resource=sql_resource)
print(entry.name)
return entry


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)

entry = datacatalog.lookup_entry(linked_resource=resource_name)
print(entry.name)
return entry


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)

entry = datacatalog.lookup_entry(sql_resource=sql_resource)
print(entry.name)
return entry


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()

if args.command == 'lookup-bigquery-dataset':
methods = {
False: lookup_bigquery_dataset,
True: lookup_bigquery_dataset_sql_resource
}
methods[args.sql_resource](args.project_id, args.dataset_id)
elif args.command == 'lookup-bigquery-table':
methods = {
False: lookup_bigquery_table,
True: lookup_bigquery_table_sql_resource
}
methods[args.sql_resource](
args.project_id, args.dataset_id, args.table_id)
elif args.command == 'lookup-pubsub-topic':
methods = {
False: lookup_pubsub_topic,
True: lookup_pubsub_topic_sql_resource
}
methods[args.sql_resource](args.project_id, args.topic_id)
66 changes: 66 additions & 0 deletions datacatalog/cloud-client/lookup_entry_test.py
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']
This conversation was marked as resolved.
Show resolved Hide resolved
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.
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)
1 change: 1 addition & 0 deletions datacatalog/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-datacatalog
This conversation was marked as resolved.
Show resolved Hide resolved