-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add samples for Data Catalog lookupEntry #1416
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
kurtisvg
merged 12 commits into
GoogleCloudPlatform:master
from
ricardolsmendes:task/add-lookup-entry-samples
May 14, 2019
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
10f515e
Add Data Catalog lookupEntry samples and tests
d4c04bd
Improve code readability
a61479d
Assert integration tests using regex
939eace
Add readme file
21f624f
Update root readme file
96f5e9e
Merge branch 'master' into task/add-lookup-entry-samples
d82c61e
Update code to keep consistecy with repo standards
95e7693
Update LookupEntryBigQueryDataset.java
aef7843
Update code to keep consistecyt with repo standards
ee527a9
Merge branch 'master' into task/add-lookup-entry-samples
96dc9c9
Update code to keep consistecy with repo standards
f36dce9
Merge branch 'master' into task/add-lookup-entry-samples
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
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,49 @@ | ||
| # Getting Started with Data Catalog and the Google Cloud Client libraries | ||
|
|
||
| <a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=datacatalog/cloud-client/README.md"> | ||
| <img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a> | ||
|
|
||
| [Data Catalog][datacatalog] is a fully managed and scalable metadata management service that empowers organizations | ||
| to quickly discover, manage, and understand all their data in Google Cloud. | ||
| This sample Java application demonstrates how to access the Data Catalog API using | ||
| the [Google Cloud Client Library for Java][google-cloud-java]. | ||
|
|
||
| [datacatalog]: https://cloud.google.com/data-catalog/ | ||
| [google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java | ||
|
|
||
| ## Quickstart | ||
|
|
||
| #### Setup | ||
| - Install [Maven](http://maven.apache.org/). | ||
| - [Enable](https://console.cloud.google.com/apis/api/datacatalog.googleapis.com/overview) Data Catalog API. | ||
| - Set up [authentication](https://cloud.google.com/docs/authentication/getting-started). | ||
|
|
||
| #### Build | ||
| - Build your project with: | ||
| ``` | ||
| mvn clean package -DskipTests | ||
| ``` | ||
|
|
||
| #### Lookup a BigQuery Dataset | ||
| ``` | ||
| mvn exec:java -Dexec.mainClass=com.example.datacatalog.LookupEntryBigQueryDataset -Dexec.args="my-project my_dataset" | ||
| ``` | ||
|
|
||
| #### Lookup a BigQuery Table | ||
| ``` | ||
| mvn exec:java -Dexec.mainClass=com.example.datacatalog.LookupEntryBigQueryTable -Dexec.args="my-project my_dataset my_table" | ||
| ``` | ||
|
|
||
| #### Lookup a Pub/Sub Topic | ||
| ``` | ||
| mvn exec:java -Dexec.mainClass=com.example.datacatalog.LookupEntryPubSubTopic -Dexec.args="my-project my-topic" | ||
| ``` | ||
|
|
||
| #### Lookup by Sql Resource | ||
| Append `-lookupBySqlResource` to command args to lookup by Sql Resource instead of Linked Resource. | ||
|
|
||
| #### Testing | ||
| Run the test with Maven. | ||
| ``` | ||
| mvn verify | ||
| ``` |
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,41 @@ | ||
| // Copyright 2019 Google Inc. | ||
| // | ||
| // 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. | ||
|
|
||
| apply plugin: 'java' | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| dependencies { | ||
| compile group: 'com.google.cloud', name: 'google-cloud-datacatalog', version:'0.4.0-alpha' | ||
|
|
||
| testCompile group: 'com.google.truth', name: 'truth', version:'0.42' | ||
| testCompile group: 'junit', name: 'junit', version:'4.13-beta-2' | ||
| } | ||
|
|
||
| test { | ||
| useJUnit() | ||
| testLogging.showStandardStreams = true | ||
| beforeTest { descriptor -> | ||
| logger.lifecycle("test: " + descriptor + " Running") | ||
| } | ||
|
|
||
| onOutput { descriptor, event -> | ||
| logger.lifecycle("test: " + descriptor + ": " + event.message ) | ||
| } | ||
| afterTest { descriptor, result -> | ||
| logger.lifecycle("test: " + descriptor + ": " + result ) | ||
| } | ||
| } |
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,72 @@ | ||
| <!-- | ||
| 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 | ||
|
|
||
| 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. | ||
| --> | ||
|
|
||
| <project> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.example.datacatalog</groupId> | ||
| <artifactId>datacatalog-google-cloud-samples</artifactId> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <!-- | ||
| The parent pom defines common style checks and testing strategies for our samples. | ||
| Removing or replacing it should not affect the execution of the samples in anyway. | ||
| --> | ||
| <parent> | ||
| <groupId>com.google.cloud.samples</groupId> | ||
| <artifactId>shared-configuration</artifactId> | ||
| <version>1.0.11</version> | ||
| </parent> | ||
|
|
||
| <properties> | ||
| <maven.compiler.target>1.8</maven.compiler.target> | ||
| <maven.compiler.source>1.8</maven.compiler.source> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-datacatalog</artifactId> | ||
| <version>0.4.0-alpha</version> | ||
| </dependency> | ||
|
|
||
| <!-- Test dependencies --> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.13-beta-2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.truth</groupId> | ||
| <artifactId>truth</artifactId> | ||
| <version>0.42</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.codehaus.mojo</groupId> | ||
| <artifactId>exec-maven-plugin</artifactId> | ||
| <version>1.6.0</version> | ||
| <configuration> | ||
| <cleanupDaemonThreads>false</cleanupDaemonThreads> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
72 changes: 72 additions & 0 deletions
72
...atalog/cloud-client/src/main/java/com/example/datacatalog/LookupEntryBigQueryDataset.java
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,72 @@ | ||
| /* | ||
| * Copyright 2019 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.example.datacatalog; | ||
|
|
||
| import com.google.cloud.datacatalog.Entry; | ||
| import com.google.cloud.datacatalog.LookupEntryRequest; | ||
| import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; | ||
|
|
||
| public class LookupEntryBigQueryDataset { | ||
|
|
||
| /** | ||
| * Lookup the Data Catalog entry referring to a BigQuery Dataset | ||
| * | ||
| * @param projectId The project ID to which the Dataset belongs, e.g. 'my-project' | ||
| * @param datasetId The dataset ID to which the Catalog Entry refers, e.g. 'my_dataset' | ||
| * @param lookupBySqlResource Indicates whether the lookup should be performed by Sql Resource | ||
| * instead of Linked Resource, e.g. 'false' | ||
| */ | ||
| private static void lookupEntryBigQueryDataset( | ||
| String projectId, String datasetId, boolean lookupBySqlResource) { | ||
|
|
||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
| LookupEntryRequest request; | ||
|
|
||
| // Construct the Lookup request to be sent by the client. | ||
This conversation was marked as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (lookupBySqlResource) { | ||
This conversation was marked as resolved.
Outdated
Show resolved
Hide resolved
|
||
| String sqlResource = String.format("bigquery.dataset.`%s`.`%s`", projectId, datasetId); | ||
| request = LookupEntryRequest.newBuilder().setSqlResource(sqlResource).build(); | ||
| } else { | ||
| String linkedResource = | ||
| String.format("//bigquery.googleapis.com/projects/%s/datasets/%s", projectId, datasetId); | ||
| request = LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build(); | ||
| } | ||
|
|
||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { | ||
| Entry entry = dataCatalogClient.lookupEntry(request); | ||
| System.out.printf("Entry name: %s\n", entry.getName()); | ||
| } catch (Exception e) { | ||
| System.out.print("Error during lookupEntryBigQueryDataset:\n" + e.toString()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Command line application to lookup the Data Catalog entry referring to a BigQuery Dataset. | ||
| * Requires 2 positional args: projectId and datasetId. A third arg is optional: | ||
| * lookupBySqlResource (when set, the lookup is done by Sql Resource instead of Linked Resource). | ||
| */ | ||
| public static void main(String... args) { | ||
This conversation was marked as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| String projectId = args[0]; | ||
| String datasetId = args[1]; | ||
| boolean lookupBySqlResource = "-lookupBySqlResource".equals(args[args.length - 1]); | ||
|
|
||
| lookupEntryBigQueryDataset(projectId, datasetId, lookupBySqlResource); | ||
| } | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
datacatalog/cloud-client/src/main/java/com/example/datacatalog/LookupEntryBigQueryTable.java
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,77 @@ | ||
| /* | ||
| * Copyright 2019 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.example.datacatalog; | ||
|
|
||
| import com.google.cloud.datacatalog.Entry; | ||
| import com.google.cloud.datacatalog.LookupEntryRequest; | ||
| import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; | ||
|
|
||
| public class LookupEntryBigQueryTable { | ||
|
|
||
| /** | ||
| * Lookup the Data Catalog entry referring to a BigQuery Table | ||
| * | ||
| * @param projectId The project ID to which the Dataset belongs, e.g. 'my-project' | ||
| * @param datasetId The dataset ID to which the Table belongs, e.g. 'my_dataset' | ||
| * @param tableId The table ID to which the Catalog Entry refers, e.g. 'my_table' | ||
| * @param lookupBySqlResource Indicates whether the lookup should be performed by Sql Resource | ||
| * instead of Linked Resource, e.g. 'false' | ||
| */ | ||
| private static void lookupEntryBigQueryTable( | ||
| String projectId, String datasetId, String tableId, boolean lookupBySqlResource) { | ||
|
|
||
| LookupEntryRequest request; | ||
|
|
||
| // Construct the Lookup request to be sent by the client. | ||
| if (lookupBySqlResource) { | ||
| String sqlResource = | ||
| String.format("bigquery.table.`%s`.`%s`.`%s`", projectId, datasetId, tableId); | ||
| request = LookupEntryRequest.newBuilder().setSqlResource(sqlResource).build(); | ||
| } else { | ||
| String linkedResource = | ||
| String.format( | ||
| "//bigquery.googleapis.com/projects/%s/datasets/%s/tables/%s", | ||
| projectId, datasetId, tableId); | ||
| request = LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build(); | ||
| } | ||
|
|
||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { | ||
| Entry entry = dataCatalogClient.lookupEntry(request); | ||
| System.out.printf("Entry name: %s\n", entry.getName()); | ||
| } catch (Exception e) { | ||
| System.out.print("Error during lookupEntryBigQueryTable:\n" + e.toString()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Command line application to lookup the Data Catalog entry referring to a BigQuery Table. | ||
| * Requires 3 positional args: projectId, datasetId, and tableId. A fourth arg is optional: | ||
| * lookupBySqlResource (when set, the lookup is done by Sql Resource instead of Linked Resource). | ||
| */ | ||
| public static void main(String... args) { | ||
|
|
||
| String projectId = args[0]; | ||
| String datasetId = args[1]; | ||
| String tableId = args[2]; | ||
| boolean lookupBySqlResource = "-lookupBySqlResource".equals(args[args.length - 1]); | ||
|
|
||
| lookupEntryBigQueryTable(projectId, datasetId, tableId, lookupBySqlResource); | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
datacatalog/cloud-client/src/main/java/com/example/datacatalog/LookupEntryPubSubTopic.java
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,72 @@ | ||
| /* | ||
| * Copyright 2019 Google Inc. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.example.datacatalog; | ||
|
|
||
| import com.google.cloud.datacatalog.Entry; | ||
| import com.google.cloud.datacatalog.LookupEntryRequest; | ||
| import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; | ||
|
|
||
| public class LookupEntryPubSubTopic { | ||
|
|
||
| /** | ||
| * Lookup the Data Catalog entry referring to a BigQuery Dataset | ||
| * | ||
| * @param projectId The project ID to which the Dataset belongs, e.g. 'my-project' | ||
| * @param topicId The topic ID to which the Catalog Entry refers, e.g. 'my-topic' | ||
| * @param lookupBySqlResource Indicates whether the lookup should be performed by Sql Resource | ||
| * instead of Linked Resource, e.g. 'false' | ||
| */ | ||
| private static void lookupEntryPubSubTopic( | ||
| String projectId, String topicId, boolean lookupBySqlResource) { | ||
|
|
||
| LookupEntryRequest request; | ||
|
|
||
| // Construct the Lookup request to be sent by the client. | ||
| if (lookupBySqlResource) { | ||
| String sqlResource = String.format("pubsub.topic.`%s`.`%s`", projectId, topicId); | ||
| request = LookupEntryRequest.newBuilder().setSqlResource(sqlResource).build(); | ||
| } else { | ||
| String linkedResource = | ||
| String.format("//pubsub.googleapis.com/projects/%s/topics/%s", projectId, topicId); | ||
| request = LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build(); | ||
| } | ||
|
|
||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { | ||
| Entry entry = dataCatalogClient.lookupEntry(request); | ||
| System.out.printf("Entry name: %s\n", entry.getName()); | ||
| } catch (Exception e) { | ||
| System.out.print("Error during lookupEntryPubSubTopic:\n" + e.toString()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Command line application to lookup the Data Catalog entry referring to a Pub/Sub Topic. | ||
| * Requires 2 positional args: projectId and topicId. A third arg is optional: lookupBySqlResource | ||
| * (when set, the lookup is done by Sql Resource instead of Linked Resource). | ||
| */ | ||
| public static void main(String... args) { | ||
|
|
||
| String projectId = args[0]; | ||
| String topicId = args[1]; | ||
| boolean lookupBySqlResource = "-lookupBySqlResource".equals(args[args.length - 1]); | ||
|
|
||
| lookupEntryPubSubTopic(projectId, topicId, lookupBySqlResource); | ||
| } | ||
| } |
Oops, something went wrong.
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.