Skip to content

Commit 15a748a

Browse files
jacspa96Jacek Spalinski
and
Jacek Spalinski
authored
feat(dataplex): add code samples for Search Entries (GoogleCloudPlatform#9609)
* feat(dataplex): add sample for search Entries * feat(dataplex): add integration tests for search Entries * feat(dataplex): make searchEntries return SearchEntriesResponse to support paging * feat(dataplex): adjust integration test based on code review * feat(dataplex): adjust comment regarding search scope * feat(dataplex): remove paging from Search --------- Co-authored-by: Jacek Spalinski <jspa@google.com>
1 parent 372f0b5 commit 15a748a

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.dataplex.v1.Entry;
23+
import java.io.IOException;
24+
import java.util.List;
25+
import java.util.UUID;
26+
import org.junit.AfterClass;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class SearchEntriesIT {
31+
private static final String ID = UUID.randomUUID().toString().substring(0, 8);
32+
private static final String LOCATION = "us-central1";
33+
private static final String entryGroupId = "test-entry-group-" + ID;
34+
private static final String entryId = "test-entry-" + ID;
35+
private static final String expectedEntry =
36+
String.format("locations/%s/entryGroups/%s/entries/%s", LOCATION, entryGroupId, entryId);
37+
38+
private static final String PROJECT_ID = requireProjectIdEnvVar();
39+
40+
private static String requireProjectIdEnvVar() {
41+
String value = System.getenv("GOOGLE_CLOUD_PROJECT");
42+
assertNotNull(
43+
"Environment variable GOOGLE_CLOUD_PROJECT is required to perform these tests.", value);
44+
return value;
45+
}
46+
47+
@BeforeClass
48+
public static void setUp() throws Exception {
49+
requireProjectIdEnvVar();
50+
CreateEntryGroup.createEntryGroup(PROJECT_ID, LOCATION, entryGroupId);
51+
CreateEntry.createEntry(PROJECT_ID, LOCATION, entryGroupId, entryId);
52+
Thread.sleep(30000);
53+
}
54+
55+
@Test
56+
public void testSearchEntries() throws IOException {
57+
String query = "name:test-entry- AND description:description AND aspect:generic";
58+
List<Entry> entries = SearchEntries.searchEntries(PROJECT_ID, query);
59+
assertThat(
60+
entries.stream()
61+
.map(Entry::getName)
62+
.map(entryName -> entryName.substring(entryName.indexOf("location"))))
63+
.contains(expectedEntry);
64+
}
65+
66+
@AfterClass
67+
public static void tearDown() throws Exception {
68+
// Entry inside this Entry Group will be deleted automatically
69+
DeleteEntryGroup.deleteEntryGroup(PROJECT_ID, LOCATION, entryGroupId);
70+
}
71+
}

0 commit comments

Comments
 (0)