-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
2065b6e
commit 4a421b0
Showing
15 changed files
with
583 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
retail/interactive-tutorials/src/main/java/events/PurgeUserEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// [START retail_purge_user_event] | ||
|
||
/* | ||
* Purge user events into a catalog from inline source using Retail API | ||
*/ | ||
|
||
package events; | ||
|
||
import static setup.SetupCleanup.writeUserEvent; | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.retail.v2.PurgeMetadata; | ||
import com.google.cloud.retail.v2.PurgeUserEventsRequest; | ||
import com.google.cloud.retail.v2.PurgeUserEventsResponse; | ||
import com.google.cloud.retail.v2.UserEventServiceClient; | ||
import java.io.IOException; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class PurgeUserEvent { | ||
|
||
public static void main(String[] args) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = System.getenv("PROJECT_ID"); | ||
String defaultCatalog = | ||
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); | ||
// visitorId generated randomly. | ||
String visitorId = UUID.randomUUID().toString(); | ||
|
||
callPurgeUserEvents(defaultCatalog, visitorId); | ||
} | ||
|
||
public static void callPurgeUserEvents(String defaultCatalog, String visitorId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
writeUserEvent(visitorId); | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) { | ||
PurgeUserEventsRequest purgeUserEventsRequest = | ||
PurgeUserEventsRequest.newBuilder() | ||
// TO CHECK ERROR HANDLING SET INVALID FILTER HERE: | ||
.setFilter(String.format("visitorId=\"%s\"", visitorId)) | ||
.setParent(defaultCatalog) | ||
.setForce(true) | ||
.build(); | ||
System.out.printf("Purge user events request: %s%n", purgeUserEventsRequest); | ||
|
||
OperationFuture<PurgeUserEventsResponse, PurgeMetadata> purgeOperation = | ||
userEventServiceClient.purgeUserEventsAsync(purgeUserEventsRequest); | ||
|
||
System.out.printf("The purge operation was started: %s%n", purgeOperation.getName()); | ||
} | ||
} | ||
} | ||
|
||
// [END retail_purge_user_event] |
77 changes: 77 additions & 0 deletions
77
retail/interactive-tutorials/src/main/java/events/RejoinUserEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// [START retail_rejoin_user_event] | ||
|
||
/* | ||
* Rejoin user events into a catalog from inline source using Retail API | ||
*/ | ||
|
||
package events; | ||
|
||
import static setup.SetupCleanup.purgeUserEvent; | ||
import static setup.SetupCleanup.writeUserEvent; | ||
|
||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.retail.v2.RejoinUserEventsMetadata; | ||
import com.google.cloud.retail.v2.RejoinUserEventsRequest; | ||
import com.google.cloud.retail.v2.RejoinUserEventsRequest.UserEventRejoinScope; | ||
import com.google.cloud.retail.v2.RejoinUserEventsResponse; | ||
import com.google.cloud.retail.v2.UserEventServiceClient; | ||
import java.io.IOException; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class RejoinUserEvent { | ||
|
||
public static void main(String[] args) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = System.getenv("PROJECT_ID"); | ||
String defaultCatalog = | ||
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); | ||
// visitorId generated randomly. | ||
String visitorId = UUID.randomUUID().toString(); | ||
|
||
callRejoinUserEvents(defaultCatalog, visitorId); | ||
} | ||
|
||
public static void callRejoinUserEvents(String defaultCatalog, String visitorId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
writeUserEvent(visitorId); | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) { | ||
RejoinUserEventsRequest rejoinUserEventsRequest = | ||
RejoinUserEventsRequest.newBuilder() | ||
.setParent(defaultCatalog) | ||
.setUserEventRejoinScope(UserEventRejoinScope.UNJOINED_EVENTS) | ||
.build(); | ||
System.out.printf("Rejoin user events request: %s%n", rejoinUserEventsRequest); | ||
|
||
OperationFuture<RejoinUserEventsResponse, RejoinUserEventsMetadata> rejoinOperation = | ||
userEventServiceClient.rejoinUserEventsAsync(rejoinUserEventsRequest); | ||
|
||
System.out.printf("The rejoin operation was started: %s%n", rejoinOperation.getName()); | ||
} | ||
|
||
purgeUserEvent(visitorId); | ||
} | ||
} | ||
|
||
// [END retail_rejoin_user_event] |
80 changes: 80 additions & 0 deletions
80
retail/interactive-tutorials/src/main/java/events/WriteUserEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// [START retail_write_user_event] | ||
|
||
/* | ||
* Write user events into a catalog from inline source using Retail API | ||
*/ | ||
|
||
package events; | ||
|
||
import static setup.SetupCleanup.purgeUserEvent; | ||
|
||
import com.google.cloud.retail.v2.UserEvent; | ||
import com.google.cloud.retail.v2.UserEventServiceClient; | ||
import com.google.cloud.retail.v2.WriteUserEventRequest; | ||
import com.google.protobuf.Timestamp; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
public class WriteUserEvent { | ||
|
||
public static void main(String[] args) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = System.getenv("PROJECT_ID"); | ||
String defaultCatalog = | ||
String.format("projects/%s/locations/global/catalogs/default_catalog", projectId); | ||
// visitorId generated randomly. | ||
String visitorId = UUID.randomUUID().toString(); | ||
|
||
writeUserEvent(defaultCatalog, visitorId); | ||
purgeUserEvent(visitorId); | ||
} | ||
|
||
public static void writeUserEvent(String defaultCatalog, String visitorId) throws IOException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (UserEventServiceClient userEventServiceClient = UserEventServiceClient.create()) { | ||
Timestamp timestamp = | ||
Timestamp.newBuilder().setSeconds(Instant.now().getEpochSecond()).build(); | ||
|
||
UserEvent userEvent = | ||
UserEvent.newBuilder() | ||
.setEventType("home-page-view") | ||
.setVisitorId(visitorId) | ||
.setEventTime(timestamp) | ||
.build(); | ||
System.out.println(userEvent); | ||
|
||
WriteUserEventRequest writeUserEventRequest = | ||
WriteUserEventRequest.newBuilder() | ||
.setUserEvent(userEvent) | ||
.setParent(defaultCatalog) | ||
.build(); | ||
System.out.printf("Write user event request: %s%n", writeUserEventRequest); | ||
|
||
userEventServiceClient.writeUserEvent(writeUserEventRequest); | ||
System.out.printf("Written user event: %s%n", userEvent); | ||
} | ||
} | ||
} | ||
|
||
// [END retail_write_user_event] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.