Skip to content

Commit 0730123

Browse files
Ricardo Mendeskurtisvg
Ricardo Mendes
authored andcommitted
Add samples for Data Catalog lookupEntry (#1416)
1 parent e2c0229 commit 0730123

File tree

8 files changed

+386
-0
lines changed

8 files changed

+386
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This is a repository that contains java code snippets on [Cloud Platform Documen
1313
Technology Samples:
1414

1515
* [Bigquery](bigquery)
16+
* [Data Catalog](datacatalog)
1617
* [Datastore](datastore)
1718
* [Endpoints](endpoints)
1819
* [Identity-Aware Proxy](iap)

datacatalog/cloud-client/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Getting Started with Data Catalog and the Google Cloud Client libraries
2+
3+
<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">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
[Data Catalog][datacatalog] is a fully managed and scalable metadata management service that empowers organizations
7+
to quickly discover, manage, and understand all their data in Google Cloud.
8+
This sample Java application demonstrates how to access the Data Catalog API using
9+
the [Google Cloud Client Library for Java][google-cloud-java].
10+
11+
[datacatalog]: https://cloud.google.com/data-catalog/
12+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
13+
14+
## Quickstart
15+
16+
#### Setup
17+
- Install [Maven](http://maven.apache.org/).
18+
- [Enable](https://console.cloud.google.com/apis/api/datacatalog.googleapis.com/overview) Data Catalog API.
19+
- Set up [authentication](https://cloud.google.com/docs/authentication/getting-started).
20+
21+
#### Build
22+
- Build your project with:
23+
```
24+
mvn clean package -DskipTests
25+
```
26+
27+
#### Testing
28+
Run the test with Maven.
29+
```
30+
mvn verify
31+
```

datacatalog/cloud-client/build.gradle

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2019 Google Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
apply plugin: 'java'
16+
17+
repositories {
18+
mavenCentral()
19+
}
20+
21+
dependencies {
22+
compile group: 'com.google.cloud', name: 'google-cloud-datacatalog', version:'0.4.0-alpha'
23+
24+
testCompile group: 'com.google.truth', name: 'truth', version:'0.42'
25+
testCompile group: 'junit', name: 'junit', version:'4.13-beta-2'
26+
}
27+
28+
test {
29+
useJUnit()
30+
testLogging.showStandardStreams = true
31+
beforeTest { descriptor ->
32+
logger.lifecycle("test: " + descriptor + " Running")
33+
}
34+
35+
onOutput { descriptor, event ->
36+
logger.lifecycle("test: " + descriptor + ": " + event.message )
37+
}
38+
afterTest { descriptor, result ->
39+
logger.lifecycle("test: " + descriptor + ": " + result )
40+
}
41+
}

datacatalog/cloud-client/pom.xml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!--
2+
Copyright 2019 Google LLC.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<project>
18+
<modelVersion>4.0.0</modelVersion>
19+
<groupId>com.example.datacatalog</groupId>
20+
<artifactId>datacatalog-google-cloud-samples</artifactId>
21+
<packaging>jar</packaging>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.11</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>1.8</maven.compiler.target>
35+
<maven.compiler.source>1.8</maven.compiler.source>
36+
</properties>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>com.google.cloud</groupId>
41+
<artifactId>google-cloud-datacatalog</artifactId>
42+
<version>0.4.0-alpha</version>
43+
</dependency>
44+
45+
<!-- Test dependencies -->
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.13-beta-2</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.google.truth</groupId>
54+
<artifactId>truth</artifactId>
55+
<version>0.42</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datacatalog;
18+
19+
import com.google.cloud.datacatalog.Entry;
20+
import com.google.cloud.datacatalog.LookupEntryRequest;
21+
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
22+
23+
public class LookupEntryBigQueryDataset {
24+
25+
/**
26+
* Lookup the Data Catalog entry referring to a BigQuery Dataset
27+
*
28+
* @param projectId The project ID to which the Dataset belongs, e.g. 'my-project'
29+
* @param datasetId The dataset ID to which the Catalog Entry refers, e.g. 'my_dataset'
30+
*/
31+
public static void lookupEntry(String projectId, String datasetId) {
32+
// String projectId = "my-project"
33+
// String datasetId = "my_dataset"
34+
35+
// Get an entry by the resource name from the source Google Cloud Platform service.
36+
String linkedResource =
37+
String.format("//bigquery.googleapis.com/projects/%s/datasets/%s", projectId, datasetId);
38+
LookupEntryRequest request =
39+
LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build();
40+
41+
// Alternatively, lookup by the SQL name of the entry would have the same result:
42+
// String sqlResource = String.format("bigquery.dataset.`%s`.`%s`", projectId, datasetId);
43+
// LookupEntryRequest request =
44+
// LookupEntryRequest.newBuilder().setSqlResource(sqlResource).build();
45+
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests. After completing all of your requests, call
48+
// the "close" method on the client to safely clean up any remaining background resources.
49+
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
50+
Entry entry = dataCatalogClient.lookupEntry(request);
51+
System.out.printf("Entry name: %s\n", entry.getName());
52+
} catch (Exception e) {
53+
System.out.print("Error during lookupEntryBigQueryDataset:\n" + e.toString());
54+
}
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datacatalog;
18+
19+
import com.google.cloud.datacatalog.Entry;
20+
import com.google.cloud.datacatalog.LookupEntryRequest;
21+
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
22+
23+
public class LookupEntryBigQueryTable {
24+
25+
/**
26+
* Lookup the Data Catalog entry referring to a BigQuery Table
27+
*
28+
* @param projectId The project ID to which the Dataset belongs, e.g. 'my-project'
29+
* @param datasetId The dataset ID to which the Table belongs, e.g. 'my_dataset'
30+
* @param tableId The table ID to which the Catalog Entry refers, e.g. 'my_table'
31+
*/
32+
public static void lookupEntry(String projectId, String datasetId, String tableId) {
33+
// String projectId = "my-project"
34+
// String datasetId = "my_dataset"
35+
// String tableId = "my_table"
36+
37+
// Get an entry by the resource name from the source Google Cloud Platform service.
38+
String linkedResource =
39+
String.format(
40+
"//bigquery.googleapis.com/projects/%s/datasets/%s/tables/%s",
41+
projectId, datasetId, tableId);
42+
LookupEntryRequest request =
43+
LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build();
44+
45+
// Alternatively, lookup by the SQL name of the entry would have the same result:
46+
// String sqlResource = String.format("bigquery.table.`%s`.`%s`.`%s`", projectId, datasetId,
47+
// tableId);
48+
// LookupEntryRequest request =
49+
// LookupEntryRequest.newBuilder().setSqlResource(sqlResource).build();
50+
51+
// Initialize client that will be used to send requests. This client only needs to be created
52+
// once, and can be reused for multiple requests. After completing all of your requests, call
53+
// the "close" method on the client to safely clean up any remaining background resources.
54+
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
55+
Entry entry = dataCatalogClient.lookupEntry(request);
56+
System.out.printf("Entry name: %s\n", entry.getName());
57+
} catch (Exception e) {
58+
System.out.print("Error during lookupEntryBigQueryTable:\n" + e.toString());
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datacatalog;
18+
19+
import com.google.cloud.datacatalog.Entry;
20+
import com.google.cloud.datacatalog.LookupEntryRequest;
21+
import com.google.cloud.datacatalog.v1beta1.DataCatalogClient;
22+
23+
public class LookupEntryPubSubTopic {
24+
25+
/**
26+
* Lookup the Data Catalog entry referring to a BigQuery Dataset
27+
*
28+
* @param projectId The project ID to which the Dataset belongs, e.g. 'my-project'
29+
* @param topicId The topic ID to which the Catalog Entry refers, e.g. 'my-topic'
30+
*/
31+
public static void lookupEntry(String projectId, String topicId) {
32+
// String projectId = "my-project"
33+
// String topicId = "my-topic"
34+
35+
// Get an entry by the resource name from the source Google Cloud Platform service.
36+
String linkedResource =
37+
String.format("//pubsub.googleapis.com/projects/%s/topics/%s", projectId, topicId);
38+
LookupEntryRequest request =
39+
LookupEntryRequest.newBuilder().setLinkedResource(linkedResource).build();
40+
41+
// Alternatively, lookup by the SQL name of the entry would have the same result:
42+
// String sqlResource = String.format("pubsub.topic.`%s`.`%s`", projectId, topicId);
43+
// LookupEntryRequest request =
44+
// LookupEntryRequest.newBuilder().setSqlResource(sqlResource).build();
45+
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests. After completing all of your requests, call
48+
// the "close" method on the client to safely clean up any remaining background resources.
49+
try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
50+
Entry entry = dataCatalogClient.lookupEntry(request);
51+
System.out.printf("Entry name: %s\n", entry.getName());
52+
} catch (Exception e) {
53+
System.out.print("Error during lookupEntryPubSubTopic:\n" + e.toString());
54+
}
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.datacatalog;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
@RunWith(JUnit4.class)
31+
public class LookupEntryTests {
32+
33+
private static final String BIGQUERY_PROJECT = "bigquery-public-data";
34+
private static final String BIGQUERY_DATASET = "new_york_taxi_trips";
35+
private static final String BIGQUERY_TABLE = "taxi_zone_geom";
36+
37+
private static final String PUBSUB_PROJECT = "pubsub-public-data";
38+
private static final String PUBSUB_TOPIC = "taxirides-realtime";
39+
40+
private ByteArrayOutputStream bout;
41+
42+
@Before
43+
public void setUp() {
44+
bout = new ByteArrayOutputStream();
45+
PrintStream out = new PrintStream(bout);
46+
System.setOut(out);
47+
}
48+
49+
@After
50+
public void tearDown() throws IOException {
51+
bout.close();
52+
System.setOut(null);
53+
}
54+
55+
@Test
56+
public void testLookupEntryBigQueryDataset() {
57+
LookupEntryBigQueryDataset.lookupEntry(BIGQUERY_PROJECT, BIGQUERY_DATASET);
58+
String got = bout.toString();
59+
assertThat(got)
60+
.containsMatch(
61+
"projects/" + BIGQUERY_PROJECT + "/locations/.+?/entryGroups/@bigquery/entries/.+?$");
62+
}
63+
64+
@Test
65+
public void testLookupEntryBigQueryTable() {
66+
LookupEntryBigQueryTable.lookupEntry(BIGQUERY_PROJECT, BIGQUERY_DATASET, BIGQUERY_TABLE);
67+
String got = bout.toString();
68+
assertThat(got)
69+
.containsMatch(
70+
"projects/" + BIGQUERY_PROJECT + "/locations/.+?/entryGroups/@bigquery/entries/.+?$");
71+
}
72+
73+
@Test
74+
public void testLookupPubSubTopic() {
75+
LookupEntryPubSubTopic.lookupEntry(PUBSUB_PROJECT, PUBSUB_TOPIC);
76+
String got = bout.toString();
77+
assertThat(got)
78+
.containsMatch(
79+
"projects/" + PUBSUB_PROJECT + "/locations/.+?/entryGroups/@pubsub/entries/.+?$");
80+
}
81+
}

0 commit comments

Comments
 (0)