Skip to content

Commit a53bcf5

Browse files
Merged changes from main
2 parents adc1ac7 + bcecf8f commit a53bcf5

File tree

7 files changed

+265
-40
lines changed

7 files changed

+265
-40
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+
}

functions/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22

33
# Google Cloud Functions Java Samples
44

5-
[Cloud Functions][functions_docs] is a lightweight, event-based, asynchronous
6-
compute solution that allows you to create small, single-purpose functions that
7-
respond to Cloud events without the need to manage a server or a runtime
8-
environment.
5+
[Cloud Run functions](https://cloud.google.com/functions/docs/concepts/overview) is a lightweight, event-based, asynchronous compute solution that allows you to create small, single-purpose functions that respond to Cloud events without the need to manage a server or a runtime environment.
96

10-
[functions_docs]: https://cloud.google.com/functions/docs/
7+
There are two versions of Cloud Run functions:
8+
9+
* **Cloud Run functions**, formerly known as Cloud Functions (2nd gen), which deploys your function as services on Cloud Run, allowing you to trigger them using Eventarc and Pub/Sub. Cloud Run functions are created using `gcloud functions` or `gcloud run`. Samples for Cloud Run functions can be found in the [`functions/v2`](v2/) folder.
10+
* **Cloud Run functions (1st gen)**, formerly known as Cloud Functions (1st gen), the original version of functions with limited event triggers and configurability. Cloud Run functions (1st gen) are created using `gcloud functions --no-gen2`. Samples for Cloud Run functions (1st generation) can be found in the current `functions/` folder.
1111

1212
## Samples
1313

1414
* [Hello World](helloworld/)
15-
* [Concepts](concepts/)
15+
* [Concepts](v2/concepts/)
1616
* [Datastore](v2/datastore/)
1717
* [Firebase](firebase/)
18-
* [Cloud Pub/Sub](pubsub/)
18+
* [Cloud Pub/Sub](v2/pubsub/)
1919
* [HTTP](http/)
2020
* [Logging & Monitoring](logging/)
2121
* [Slack](slack/)
22-
* [OCR tutorial](ocr/)
23-
* [ImageMagick](imagemagick/)
22+
* [OCR tutorial](v2/ocr/)
23+
* [ImageMagick](v2/imagemagick/)
2424
* [CI/CD setup](ci_cd/)
2525

2626
## Running Functions Locally

tpu/src/main/java/tpu/CreateTimeBoundQueuedResource.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
package tpu;
1818

1919
// [START tpu_queued_resources_time_bound]
20-
import com.google.api.gax.retrying.RetrySettings;
2120
import com.google.cloud.tpu.v2alpha1.CreateQueuedResourceRequest;
2221
import com.google.cloud.tpu.v2alpha1.Node;
2322
import com.google.cloud.tpu.v2alpha1.QueuedResource;
2423
import com.google.cloud.tpu.v2alpha1.TpuClient;
25-
import com.google.cloud.tpu.v2alpha1.TpuSettings;
2624
import com.google.protobuf.Duration;
2725
// Uncomment the following line to use Interval or Date
2826
//import com.google.protobuf.Timestamp;
@@ -65,24 +63,9 @@ public static QueuedResource createTimeBoundQueuedResource(
6563
String projectId, String nodeName, String queuedResourceName,
6664
String zone, String tpuType, String tpuSoftwareVersion)
6765
throws IOException, ExecutionException, InterruptedException {
68-
// With these settings the client library handles the Operation's polling mechanism
69-
// and prevent CancellationException error
70-
TpuSettings.Builder clientSettings =
71-
TpuSettings.newBuilder();
72-
clientSettings
73-
.createQueuedResourceSettings()
74-
.setRetrySettings(
75-
RetrySettings.newBuilder()
76-
.setInitialRetryDelay(org.threeten.bp.Duration.ofMillis(5000L))
77-
.setRetryDelayMultiplier(2.0)
78-
.setInitialRpcTimeout(org.threeten.bp.Duration.ZERO)
79-
.setRpcTimeoutMultiplier(1.0)
80-
.setMaxRetryDelay(org.threeten.bp.Duration.ofMillis(45000L))
81-
.setTotalTimeout(org.threeten.bp.Duration.ofHours(24L))
82-
.build());
8366
// Initialize client that will be used to send requests. This client only needs to be created
8467
// once, and can be reused for multiple requests.
85-
try (TpuClient tpuClient = TpuClient.create(clientSettings.build())) {
68+
try (TpuClient tpuClient = TpuClient.create()) {
8669
// Define parent for requests
8770
String parent = String.format("projects/%s/locations/%s", projectId, zone);
8871
// Create a Duration object representing 6 hours.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 tpu;
18+
19+
//[START tpu_vm_create_topology]
20+
import com.google.cloud.tpu.v2.AcceleratorConfig;
21+
import com.google.cloud.tpu.v2.AcceleratorConfig.Type;
22+
import com.google.cloud.tpu.v2.CreateNodeRequest;
23+
import com.google.cloud.tpu.v2.Node;
24+
import com.google.cloud.tpu.v2.TpuClient;
25+
import java.io.IOException;
26+
import java.util.concurrent.ExecutionException;
27+
28+
public class CreateTpuWithTopologyFlag {
29+
30+
public static void main(String[] args)
31+
throws IOException, ExecutionException, InterruptedException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
// Project ID or project number of the Google Cloud project you want to create a node.
34+
String projectId = "YOUR_PROJECT_ID";
35+
// The zone in which to create the TPU.
36+
// For more information about supported TPU types for specific zones,
37+
// see https://cloud.google.com/tpu/docs/regions-zones
38+
String zone = "europe-west4-a";
39+
// The name for your TPU.
40+
String nodeName = "YOUR_TPU_NAME";
41+
// The version of the Cloud TPU you want to create.
42+
// Available options: TYPE_UNSPECIFIED = 0, V2 = 2, V3 = 4, V4 = 7
43+
Type tpuVersion = AcceleratorConfig.Type.V2;
44+
// Software version that specifies the version of the TPU runtime to install.
45+
// For more information, see https://cloud.google.com/tpu/docs/runtimes
46+
String tpuSoftwareVersion = "tpu-vm-tf-2.17.0-pod-pjrt";
47+
// The physical topology of your TPU slice.
48+
// For more information about topology for each TPU version,
49+
// see https://cloud.google.com/tpu/docs/system-architecture-tpu-vm#versions.
50+
String topology = "2x2";
51+
52+
createTpuWithTopologyFlag(projectId, zone, nodeName, tpuVersion, tpuSoftwareVersion, topology);
53+
}
54+
55+
// Creates a TPU VM with the specified name, zone, version and topology.
56+
public static Node createTpuWithTopologyFlag(String projectId, String zone, String nodeName,
57+
Type tpuVersion, String tpuSoftwareVersion, String topology)
58+
throws IOException, ExecutionException, InterruptedException {
59+
// Initialize client that will be used to send requests. This client only needs to be created
60+
// once, and can be reused for multiple requests.
61+
try (TpuClient tpuClient = TpuClient.create()) {
62+
String parent = String.format("projects/%s/locations/%s", projectId, zone);
63+
Node tpuVm =
64+
Node.newBuilder()
65+
.setName(nodeName)
66+
.setAcceleratorConfig(Node.newBuilder()
67+
.getAcceleratorConfigBuilder()
68+
.setType(tpuVersion)
69+
.setTopology(topology)
70+
.build())
71+
.setRuntimeVersion(tpuSoftwareVersion)
72+
.build();
73+
74+
CreateNodeRequest request =
75+
CreateNodeRequest.newBuilder()
76+
.setParent(parent)
77+
.setNodeId(nodeName)
78+
.setNode(tpuVm)
79+
.build();
80+
81+
return tpuClient.createNodeAsync(request).get();
82+
}
83+
}
84+
}
85+
//[END tpu_vm_create_topology]

tpu/src/test/java/tpu/QueuedResourceIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import org.mockito.MockedStatic;
4444

4545
@RunWith(JUnit4.class)
46-
@Timeout(value = 30)
46+
@Timeout(value = 10)
4747
public class QueuedResourceIT {
4848
private static final String PROJECT_ID = "project-id";
4949
private static final String ZONE = "europe-west4-a";
@@ -131,8 +131,7 @@ public void testCreateTimeBoundQueuedResource() throws Exception {
131131
TpuClient mockTpuClient = mock(TpuClient.class);
132132
OperationFuture mockFuture = mock(OperationFuture.class);
133133

134-
mockedTpuClient.when(() -> TpuClient.create(any(TpuSettings.class)))
135-
.thenReturn(mockTpuClient);
134+
mockedTpuClient.when(TpuClient::create).thenReturn(mockTpuClient);
136135
when(mockTpuClient.createQueuedResourceAsync(any(CreateQueuedResourceRequest.class)))
137136
.thenReturn(mockFuture);
138137
when(mockFuture.get()).thenReturn(mockQueuedResource);

0 commit comments

Comments
 (0)