|
| 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 | +} |
0 commit comments