|
| 1 | +/* |
| 2 | + * Copyright 2024 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 | +package dataplex; |
| 18 | + |
| 19 | +// [START dataplex_search_entries] |
| 20 | +import com.google.cloud.dataplex.v1.CatalogServiceClient; |
| 21 | +import com.google.cloud.dataplex.v1.Entry; |
| 22 | +import com.google.cloud.dataplex.v1.SearchEntriesRequest; |
| 23 | +import com.google.cloud.dataplex.v1.SearchEntriesResult; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.List; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +public class SearchEntries { |
| 29 | + |
| 30 | + public static void main(String[] args) throws IOException { |
| 31 | + // TODO(developer): Replace these variables before running the sample. |
| 32 | + String projectId = "MY_PROJECT_ID"; |
| 33 | + // How to write query for search: https://cloud.google.com/dataplex/docs/search-syntax |
| 34 | + String query = "MY_QUERY"; |
| 35 | + |
| 36 | + List<Entry> entries = searchEntries(projectId, query); |
| 37 | + entries.forEach(entry -> System.out.println("Entry name found in search: " + entry.getName())); |
| 38 | + } |
| 39 | + |
| 40 | + // Method to search Entries located in projectId and matching query |
| 41 | + public static List<Entry> searchEntries(String projectId, String query) throws IOException { |
| 42 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 43 | + // once, and can be reused for multiple requests. |
| 44 | + try (CatalogServiceClient client = CatalogServiceClient.create()) { |
| 45 | + SearchEntriesRequest searchEntriesRequest = |
| 46 | + SearchEntriesRequest.newBuilder() |
| 47 | + .setPageSize(100) |
| 48 | + // Required field, will by default limit search scope to organization under which the |
| 49 | + // project is located |
| 50 | + .setName(String.format("projects/%s/locations/global", projectId)) |
| 51 | + // Optional field, will further limit search scope only to specified project |
| 52 | + .setScope(String.format("projects/%s", projectId)) |
| 53 | + .setQuery(query) |
| 54 | + .build(); |
| 55 | + |
| 56 | + CatalogServiceClient.SearchEntriesPagedResponse searchEntriesResponse = |
| 57 | + client.searchEntries(searchEntriesRequest); |
| 58 | + return searchEntriesResponse.getPage().getResponse().getResultsList().stream() |
| 59 | + // Extract Entries nested inside search results |
| 60 | + .map(SearchEntriesResult::getDataplexEntry) |
| 61 | + .collect(Collectors.toList()); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +// [END dataplex_search_entries] |
0 commit comments