-
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 all 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,31 @@ | ||
# 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 | ||
``` | ||
|
||
#### 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,59 @@ | ||
<!-- | ||
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> | ||
</project> |
56 changes: 56 additions & 0 deletions
56
...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,56 @@ | ||
/* | ||
* 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' | ||
*/ | ||
public static void lookupEntry(String projectId, String datasetId) { | ||
// String projectId = "my-project" | ||
// String datasetId = "my_dataset" | ||
|
||
// Get an entry by the resource name from the source Google Cloud Platform service. | ||
String linkedResource = | ||
String.format("//bigquery.googleapis.com/projects/%s/datasets/%s", projectId, datasetId); | ||
LookupEntryRequest request = | ||
LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build(); | ||
|
||
// Alternatively, lookup by the SQL name of the entry would have the same result: | ||
// String sqlResource = String.format("bigquery.dataset.`%s`.`%s`", projectId, datasetId); | ||
// LookupEntryRequest request = | ||
// LookupEntryRequest.newBuilder().setSqlResource(sqlResource).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()); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
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,61 @@ | ||
/* | ||
* 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' | ||
*/ | ||
public static void lookupEntry(String projectId, String datasetId, String tableId) { | ||
// String projectId = "my-project" | ||
// String datasetId = "my_dataset" | ||
// String tableId = "my_table" | ||
|
||
// Get an entry by the resource name from the source Google Cloud Platform service. | ||
String linkedResource = | ||
String.format( | ||
"//bigquery.googleapis.com/projects/%s/datasets/%s/tables/%s", | ||
projectId, datasetId, tableId); | ||
LookupEntryRequest request = | ||
LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build(); | ||
|
||
// Alternatively, lookup by the SQL name of the entry would have the same result: | ||
// String sqlResource = String.format("bigquery.table.`%s`.`%s`.`%s`", projectId, datasetId, | ||
// tableId); | ||
// LookupEntryRequest request = | ||
// LookupEntryRequest.newBuilder().setSqlResource(sqlResource).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()); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
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,56 @@ | ||
/* | ||
* 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' | ||
*/ | ||
public static void lookupEntry(String projectId, String topicId) { | ||
// String projectId = "my-project" | ||
// String topicId = "my-topic" | ||
|
||
// Get an entry by the resource name from the source Google Cloud Platform service. | ||
String linkedResource = | ||
String.format("//pubsub.googleapis.com/projects/%s/topics/%s", projectId, topicId); | ||
LookupEntryRequest request = | ||
LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build(); | ||
|
||
// Alternatively, lookup by the SQL name of the entry would have the same result: | ||
// String sqlResource = String.format("pubsub.topic.`%s`.`%s`", projectId, topicId); | ||
// LookupEntryRequest request = | ||
// LookupEntryRequest.newBuilder().setSqlResource(sqlResource).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()); | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
datacatalog/cloud-client/src/test/java/com/example/datacatalog/LookupEntryTests.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,81 @@ | ||
/* | ||
* 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 static com.google.common.truth.Truth.assertThat; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class LookupEntryTests { | ||
|
||
private static final String BIGQUERY_PROJECT = "bigquery-public-data"; | ||
private static final String BIGQUERY_DATASET = "new_york_taxi_trips"; | ||
private static final String BIGQUERY_TABLE = "taxi_zone_geom"; | ||
|
||
private static final String PUBSUB_PROJECT = "pubsub-public-data"; | ||
private static final String PUBSUB_TOPIC = "taxirides-realtime"; | ||
|
||
private ByteArrayOutputStream bout; | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
PrintStream out = new PrintStream(bout); | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
bout.close(); | ||
System.setOut(null); | ||
} | ||
|
||
@Test | ||
public void testLookupEntryBigQueryDataset() { | ||
LookupEntryBigQueryDataset.lookupEntry(BIGQUERY_PROJECT, BIGQUERY_DATASET); | ||
String got = bout.toString(); | ||
assertThat(got) | ||
.containsMatch( | ||
"projects/" + BIGQUERY_PROJECT + "/locations/.+?/entryGroups/@bigquery/entries/.+?$"); | ||
} | ||
|
||
@Test | ||
public void testLookupEntryBigQueryTable() { | ||
LookupEntryBigQueryTable.lookupEntry(BIGQUERY_PROJECT, BIGQUERY_DATASET, BIGQUERY_TABLE); | ||
String got = bout.toString(); | ||
assertThat(got) | ||
.containsMatch( | ||
"projects/" + BIGQUERY_PROJECT + "/locations/.+?/entryGroups/@bigquery/entries/.+?$"); | ||
} | ||
|
||
@Test | ||
public void testLookupPubSubTopic() { | ||
LookupEntryPubSubTopic.lookupEntry(PUBSUB_PROJECT, PUBSUB_TOPIC); | ||
String got = bout.toString(); | ||
assertThat(got) | ||
.containsMatch( | ||
"projects/" + PUBSUB_PROJECT + "/locations/.+?/entryGroups/@pubsub/entries/.+?$"); | ||
} | ||
} |
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.