|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | + * https://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 | +// DO NOT EDIT! This is a generated sample ("RequestPaged", "datacatalog_search") |
| 17 | +// sample-metadata: |
| 18 | +// title: |
| 19 | +// description: Search Catalog |
| 20 | +// usage: gradle run -PmainClass=com.google.cloud.examples.datacatalog.v1beta1.DatacatalogSearch |
| 21 | +// [--args='[--include_project_id "[Google Cloud Project ID]"] [--include_gcp_public_datasets false] |
| 22 | +// [--query "[String in search query syntax]"]'] |
| 23 | + |
| 24 | +package com.google.cloud.examples.datacatalog.v1beta1; |
| 25 | + |
| 26 | +import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; |
| 27 | +import com.google.cloud.datacatalog.v1beta1.SearchCatalogRequest; |
| 28 | +import com.google.cloud.datacatalog.v1beta1.SearchCatalogResult; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.List; |
| 31 | +import org.apache.commons.cli.CommandLine; |
| 32 | +import org.apache.commons.cli.DefaultParser; |
| 33 | +import org.apache.commons.cli.Option; |
| 34 | +import org.apache.commons.cli.Options; |
| 35 | + |
| 36 | +public class DatacatalogSearch { |
| 37 | + // [START datacatalog_search] |
| 38 | + /* |
| 39 | + * Please include the following imports to run this sample. |
| 40 | + * |
| 41 | + * import com.google.cloud.datacatalog.v1beta1.DataCatalogClient; |
| 42 | + * import com.google.cloud.datacatalog.v1beta1.SearchCatalogRequest; |
| 43 | + * import com.google.cloud.datacatalog.v1beta1.SearchCatalogRequest.Scope; |
| 44 | + * import com.google.cloud.datacatalog.v1beta1.SearchCatalogResult; |
| 45 | + * import java.util.Arrays; |
| 46 | + * import java.util.List; |
| 47 | + */ |
| 48 | + |
| 49 | + public static void sampleSearchCatalog() { |
| 50 | + // TODO(developer): Replace these variables before running the sample. |
| 51 | + String includeProjectId = "[Google Cloud Project ID]"; |
| 52 | + boolean includeGcpPublicDatasets = false; |
| 53 | + String query = "[String in search query syntax]"; |
| 54 | + sampleSearchCatalog(includeProjectId, includeGcpPublicDatasets, query); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Search Catalog |
| 59 | + * |
| 60 | + * @param includeProjectId Your Google Cloud project ID. |
| 61 | + * @param includeGcpPublicDatasets If true, include Google Cloud Platform (GCP) public datasets in |
| 62 | + * the search results. |
| 63 | + * @param query Your query string. See: |
| 64 | + * https://cloud.google.com/data-catalog/docs/how-to/search-reference Example: system=bigquery |
| 65 | + * type=dataset |
| 66 | + */ |
| 67 | + public static void sampleSearchCatalog( |
| 68 | + String includeProjectId, boolean includeGcpPublicDatasets, String query) { |
| 69 | + try (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) { |
| 70 | + List<String> includeProjectIds = Arrays.asList(includeProjectId); |
| 71 | + SearchCatalogRequest.Scope scope = |
| 72 | + SearchCatalogRequest.Scope.newBuilder() |
| 73 | + .addAllIncludeProjectIds(includeProjectIds) |
| 74 | + .setIncludeGcpPublicDatasets(includeGcpPublicDatasets) |
| 75 | + .build(); |
| 76 | + SearchCatalogRequest request = |
| 77 | + SearchCatalogRequest.newBuilder().setScope(scope).setQuery(query).build(); |
| 78 | + for (SearchCatalogResult responseItem : |
| 79 | + dataCatalogClient.searchCatalog(request).iterateAll()) { |
| 80 | + System.out.printf("Result type: %s\n", responseItem.getSearchResultType()); |
| 81 | + System.out.printf("Result subtype: %s\n", responseItem.getSearchResultSubtype()); |
| 82 | + System.out.printf("Relative resource name: %s\n", responseItem.getRelativeResourceName()); |
| 83 | + System.out.printf("Linked resource: %s\n\n", responseItem.getLinkedResource()); |
| 84 | + } |
| 85 | + } catch (Exception exception) { |
| 86 | + System.err.println("Failed to create the client due to: " + exception); |
| 87 | + } |
| 88 | + } |
| 89 | + // [END datacatalog_search] |
| 90 | + |
| 91 | + public static void main(String[] args) throws Exception { |
| 92 | + Options options = new Options(); |
| 93 | + options.addOption( |
| 94 | + Option.builder("").required(false).hasArg(true).longOpt("include_project_id").build()); |
| 95 | + options.addOption( |
| 96 | + Option.builder("") |
| 97 | + .required(false) |
| 98 | + .hasArg(true) |
| 99 | + .longOpt("include_gcp_public_datasets") |
| 100 | + .build()); |
| 101 | + options.addOption(Option.builder("").required(false).hasArg(true).longOpt("query").build()); |
| 102 | + |
| 103 | + CommandLine cl = (new DefaultParser()).parse(options, args); |
| 104 | + String includeProjectId = cl.getOptionValue("include_project_id", "[Google Cloud Project ID]"); |
| 105 | + boolean includeGcpPublicDatasets = |
| 106 | + cl.getOptionValue("include_gcp_public_datasets") != null |
| 107 | + ? Boolean.parseBoolean(cl.getOptionValue("include_gcp_public_datasets")) |
| 108 | + : false; |
| 109 | + String query = cl.getOptionValue("query", "[String in search query syntax]"); |
| 110 | + |
| 111 | + sampleSearchCatalog(includeProjectId, includeGcpPublicDatasets, query); |
| 112 | + } |
| 113 | +} |
0 commit comments