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 lookupEntry #1416

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
31 changes: 31 additions & 0 deletions datacatalog/cloud-client/README.md
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
```
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>
This conversation was marked as resolved.
Show resolved Hide resolved
<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,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"

This conversation was marked as resolved.
Show resolved Hide resolved
// 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());
}
}
}
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());
}
}
}
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());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.datacatalog;

public class QuickStart {
This conversation was marked as resolved.
Show resolved Hide resolved
}
Loading