Skip to content

Commit 23c7054

Browse files
Ricardo Mendeschingor13
authored andcommitted
samples: Add samples for Data Catalog lookupEntry (#1416)
1 parent 7cbc559 commit 23c7054

File tree

4 files changed

+254
-0
lines changed

4 files changed

+254
-0
lines changed
Lines changed: 56 additions & 0 deletions
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+
}
Lines changed: 61 additions & 0 deletions
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+
}
Lines changed: 56 additions & 0 deletions
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+
}
Lines changed: 81 additions & 0 deletions
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)