Skip to content

Commit 4a421b0

Browse files
sborisenkoxgcf-owl-bot[bot]Neenu1995
authored
chore(samples): Retail Tutorials. Events (write, rejoin, purge) (#303)
* Add user events: write, rejoin, purge. * Add kokoro configuration. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fixes. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Refactor code. * Tests fixes. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Refactoring code. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fix test fails. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fix test fails. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fix tests. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Neenu Shaji <[email protected]>
1 parent 2065b6e commit 4a421b0

15 files changed

+583
-16
lines changed

retail/interactive-tutorials/pom.xml

+19-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2626
</properties>
2727

28+
<dependencyManagement>
29+
<dependencies>
30+
<dependency>
31+
<groupId>com.google.cloud</groupId>
32+
<artifactId>libraries-bom</artifactId>
33+
<version>24.3.0</version>
34+
<type>pom</type>
35+
<scope>import</scope>
36+
</dependency>
37+
</dependencies>
38+
</dependencyManagement>
39+
2840
<dependencies>
2941
<dependency>
3042
<groupId>com.google.cloud</groupId>
@@ -52,6 +64,12 @@
5264
<artifactId>gson</artifactId>
5365
<version>2.9.0</version>
5466
</dependency>
67+
<dependency>
68+
<groupId>com.google.truth</groupId>
69+
<artifactId>truth</artifactId>
70+
<version>1.1.3</version>
71+
<scope>test</scope>
72+
</dependency>
5573
</dependencies>
5674

5775
<build>
@@ -61,7 +79,7 @@
6179
<artifactId>exec-maven-plugin</artifactId>
6280
<version>3.0.0</version>
6381
<configuration>
64-
<cleanupDaemonThreads>false</cleanupDaemonThreads>
82+
<cleanupDaemonThreads>true</cleanupDaemonThreads>
6583
</configuration>
6684
</plugin>
6785
</plugins>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2022 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+
// [START retail_purge_user_event]
18+
19+
/*
20+
* Purge user events into a catalog from inline source using Retail API
21+
*/
22+
23+
package events;
24+
25+
import static setup.SetupCleanup.writeUserEvent;
26+
27+
import com.google.api.gax.longrunning.OperationFuture;
28+
import com.google.cloud.retail.v2.PurgeMetadata;
29+
import com.google.cloud.retail.v2.PurgeUserEventsRequest;
30+
import com.google.cloud.retail.v2.PurgeUserEventsResponse;
31+
import com.google.cloud.retail.v2.UserEventServiceClient;
32+
import java.io.IOException;
33+
import java.util.UUID;
34+
import java.util.concurrent.ExecutionException;
35+
36+
public class PurgeUserEvent {
37+
38+
public static void main(String[] args)
39+
throws IOException, ExecutionException, InterruptedException {
40+
// TODO(developer): Replace these variables before running the sample.
41+
String projectId = System.getenv("PROJECT_ID");
42+
String defaultCatalog =
43+
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
44+
// visitorId generated randomly.
45+
String visitorId = UUID.randomUUID().toString();
46+
47+
callPurgeUserEvents(defaultCatalog, visitorId);
48+
}
49+
50+
public static void callPurgeUserEvents(String defaultCatalog, String visitorId)
51+
throws IOException, ExecutionException, InterruptedException {
52+
writeUserEvent(visitorId);
53+
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests. After completing all of your requests, call
56+
// the "close" method on the client to safely clean up any remaining background resources.
57+
try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) {
58+
PurgeUserEventsRequest purgeUserEventsRequest =
59+
PurgeUserEventsRequest.newBuilder()
60+
// TO CHECK ERROR HANDLING SET INVALID FILTER HERE:
61+
.setFilter(String.format("visitorId=\"%s\"", visitorId))
62+
.setParent(defaultCatalog)
63+
.setForce(true)
64+
.build();
65+
System.out.printf("Purge user events request: %s%n", purgeUserEventsRequest);
66+
67+
OperationFuture<PurgeUserEventsResponse, PurgeMetadata> purgeOperation =
68+
userEventServiceClient.purgeUserEventsAsync(purgeUserEventsRequest);
69+
70+
System.out.printf("The purge operation was started: %s%n", purgeOperation.getName());
71+
}
72+
}
73+
}
74+
75+
// [END retail_purge_user_event]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2022 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+
// [START retail_rejoin_user_event]
18+
19+
/*
20+
* Rejoin user events into a catalog from inline source using Retail API
21+
*/
22+
23+
package events;
24+
25+
import static setup.SetupCleanup.purgeUserEvent;
26+
import static setup.SetupCleanup.writeUserEvent;
27+
28+
import com.google.api.gax.longrunning.OperationFuture;
29+
import com.google.cloud.retail.v2.RejoinUserEventsMetadata;
30+
import com.google.cloud.retail.v2.RejoinUserEventsRequest;
31+
import com.google.cloud.retail.v2.RejoinUserEventsRequest.UserEventRejoinScope;
32+
import com.google.cloud.retail.v2.RejoinUserEventsResponse;
33+
import com.google.cloud.retail.v2.UserEventServiceClient;
34+
import java.io.IOException;
35+
import java.util.UUID;
36+
import java.util.concurrent.ExecutionException;
37+
38+
public class RejoinUserEvent {
39+
40+
public static void main(String[] args)
41+
throws IOException, ExecutionException, InterruptedException {
42+
// TODO(developer): Replace these variables before running the sample.
43+
String projectId = System.getenv("PROJECT_ID");
44+
String defaultCatalog =
45+
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
46+
// visitorId generated randomly.
47+
String visitorId = UUID.randomUUID().toString();
48+
49+
callRejoinUserEvents(defaultCatalog, visitorId);
50+
}
51+
52+
public static void callRejoinUserEvents(String defaultCatalog, String visitorId)
53+
throws IOException, ExecutionException, InterruptedException {
54+
writeUserEvent(visitorId);
55+
56+
// Initialize client that will be used to send requests. This client only needs to be created
57+
// once, and can be reused for multiple requests. After completing all of your requests, call
58+
// the "close" method on the client to safely clean up any remaining background resources.
59+
try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) {
60+
RejoinUserEventsRequest rejoinUserEventsRequest =
61+
RejoinUserEventsRequest.newBuilder()
62+
.setParent(defaultCatalog)
63+
.setUserEventRejoinScope(UserEventRejoinScope.UNJOINED_EVENTS)
64+
.build();
65+
System.out.printf("Rejoin user events request: %s%n", rejoinUserEventsRequest);
66+
67+
OperationFuture<RejoinUserEventsResponse, RejoinUserEventsMetadata> rejoinOperation =
68+
userEventServiceClient.rejoinUserEventsAsync(rejoinUserEventsRequest);
69+
70+
System.out.printf("The rejoin operation was started: %s%n", rejoinOperation.getName());
71+
}
72+
73+
purgeUserEvent(visitorId);
74+
}
75+
}
76+
77+
// [END retail_rejoin_user_event]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2022 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+
// [START retail_write_user_event]
18+
19+
/*
20+
* Write user events into a catalog from inline source using Retail API
21+
*/
22+
23+
package events;
24+
25+
import static setup.SetupCleanup.purgeUserEvent;
26+
27+
import com.google.cloud.retail.v2.UserEvent;
28+
import com.google.cloud.retail.v2.UserEventServiceClient;
29+
import com.google.cloud.retail.v2.WriteUserEventRequest;
30+
import com.google.protobuf.Timestamp;
31+
import java.io.IOException;
32+
import java.time.Instant;
33+
import java.util.UUID;
34+
import java.util.concurrent.ExecutionException;
35+
36+
public class WriteUserEvent {
37+
38+
public static void main(String[] args)
39+
throws IOException, ExecutionException, InterruptedException {
40+
// TODO(developer): Replace these variables before running the sample.
41+
String projectId = System.getenv("PROJECT_ID");
42+
String defaultCatalog =
43+
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
44+
// visitorId generated randomly.
45+
String visitorId = UUID.randomUUID().toString();
46+
47+
writeUserEvent(defaultCatalog, visitorId);
48+
purgeUserEvent(visitorId);
49+
}
50+
51+
public static void writeUserEvent(String defaultCatalog, String visitorId) throws IOException {
52+
// Initialize client that will be used to send requests. This client only needs to be created
53+
// once, and can be reused for multiple requests. After completing all of your requests, call
54+
// the "close" method on the client to safely clean up any remaining background resources.
55+
try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) {
56+
Timestamp timestamp =
57+
Timestamp.newBuilder().setSeconds(Instant.now().getEpochSecond()).build();
58+
59+
UserEvent userEvent =
60+
UserEvent.newBuilder()
61+
.setEventType("home-page-view")
62+
.setVisitorId(visitorId)
63+
.setEventTime(timestamp)
64+
.build();
65+
System.out.println(userEvent);
66+
67+
WriteUserEventRequest writeUserEventRequest =
68+
WriteUserEventRequest.newBuilder()
69+
.setUserEvent(userEvent)
70+
.setParent(defaultCatalog)
71+
.build();
72+
System.out.printf("Write user event request: %s%n", writeUserEventRequest);
73+
74+
userEventServiceClient.writeUserEvent(writeUserEventRequest);
75+
System.out.printf("Written user event: %s%n", userEvent);
76+
}
77+
}
78+
}
79+
80+
// [END retail_write_user_event]

retail/interactive-tutorials/src/main/java/product/AddFulfillmentPlaces.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public static void main(String[] args) throws IOException, InterruptedException
3939
String generatedProductId = UUID.randomUUID().toString();
4040
String productName =
4141
String.format(
42-
"projects/%s/locations/global/catalogs/default_catalog/branches/"
43-
+ "default_branch/products/%s",
42+
"projects/%s/locations/global/catalogs/default_catalog/branches/" + "0/products/%s",
4443
projectId, generatedProductId);
4544
Timestamp currentDate =
4645
Timestamp.newBuilder()

retail/interactive-tutorials/src/main/java/product/ImportProductsBigQueryTable.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public class ImportProductsBigQueryTable {
3838
private static final String PROJECT_ID = System.getenv("PROJECT_ID");
3939
private static final String DEFAULT_CATALOG =
4040
String.format(
41-
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch",
42-
PROJECT_ID);
41+
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID);
4342
private static final String DATASET_ID = "products";
4443
private static final String TABLE_ID = "products";
4544
// TO CHECK ERROR HANDLING USE THE TABLE WITH INVALID PRODUCTS:

retail/interactive-tutorials/src/main/java/product/ImportProductsGcs.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public class ImportProductsGcs {
4040
private static final String PROJECT_ID = System.getenv("PROJECT_ID");
4141
private static final String DEFAULT_CATALOG =
4242
String.format(
43-
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch",
44-
PROJECT_ID);
43+
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID);
4544
private static final String GCS_BUCKET = String.format("gs://%s", System.getenv("BUCKET_NAME"));
4645
private static final String GCS_ERROR_BUCKET = String.format("%s/errors", GCS_BUCKET);
4746
private static final String GCS_PRODUCTS_OBJECT = "products.json";

retail/interactive-tutorials/src/main/java/product/ImportProductsInlineSource.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public class ImportProductsInlineSource {
4747
private static final String PROJECT_ID = System.getenv("PROJECT_ID");
4848
private static final String DEFAULT_CATALOG =
4949
String.format(
50-
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch",
51-
PROJECT_ID);
50+
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID);
5251

5352
public static void main(String[] args) throws IOException, InterruptedException {
5453
ImportProductsRequest importRequest = getImportProductsInlineRequest(getProducts());

retail/interactive-tutorials/src/main/java/product/RemoveFulfillmentPlaces.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public static void main(String[] args) throws IOException, InterruptedException
4040
String generatedProductId = UUID.randomUUID().toString();
4141
String productName =
4242
String.format(
43-
"projects/%s/locations/global/catalogs/default_catalog/branches/"
44-
+ "default_branch/products/%s",
43+
"projects/%s/locations/global/catalogs/default_catalog/branches/" + "0/products/%s",
4544
projectId, generatedProductId);
4645
Timestamp currentDate =
4746
Timestamp.newBuilder()

retail/interactive-tutorials/src/main/java/product/SetInventory.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public static void main(String[] args) throws IOException, InterruptedException
4646
String generatedProductId = UUID.randomUUID().toString();
4747
String productName =
4848
String.format(
49-
"projects/%s/locations/global/catalogs/default_catalog/"
50-
+ "branches/default_branch/products/%s",
49+
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/0/products/%s",
5150
projectId, generatedProductId);
5251

5352
createProduct(generatedProductId);

0 commit comments

Comments
 (0)