Skip to content

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

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This is a repository that contains java code snippets on [Cloud Platform Documen
Technology Samples:

* [Bigquery](bigquery)
* [Data Catalog](datacatalog)
* [Datastore](datastore)
* [Endpoints](endpoints)
* [Identity-Aware Proxy](iap)
Expand Down
49 changes: 49 additions & 0 deletions datacatalog/cloud-client/README.md
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
```
41 changes: 41 additions & 0 deletions datacatalog/cloud-client/build.gradle
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 )
}
}
72 changes: 72 additions & 0 deletions datacatalog/cloud-client/pom.xml
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>
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) {

LookupEntryRequest request;

// Construct the Lookup request to be sent by the client.
if (lookupBySqlResource) {
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) {

String projectId = args[0];
String datasetId = args[1];
boolean lookupBySqlResource = "-lookupBySqlResource".equals(args[args.length - 1]);

lookupEntryBigQueryDataset(projectId, datasetId, lookupBySqlResource);
}
}
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);
}
}
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);
}
}
Loading