This repository was archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
chore(samples): Retail Tutorials. Events (write, rejoin, purge) #303
Merged
kweinmeister
merged 21 commits into
googleapis:main
from
sborisenkox:crs-tutorials-events-crud
Mar 16, 2022
Merged
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
47390ed
Add user events: write, rejoin, purge.
sborisenkox cb2d502
Add kokoro configuration.
sborisenkox d8e0c24
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] cb7df0a
Fixes.
sborisenkox a59d185
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 4f1bd2a
Refactor code.
sborisenkox 2043308
Tests fixes.
sborisenkox 6af1997
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 7e494dc
Merge branch 'main' into crs-tutorials-events-crud
Neenu1995 5fb6e9e
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] ead78b2
Refactoring code.
sborisenkox 7a28d4a
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 7c76b49
Fix test fails.
sborisenkox 3187000
Merge branch 'test-fixes' into crs-tutorials-events-crud
sborisenkox d47baec
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] eb92a3b
Fix test fails.
sborisenkox d795c9e
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] a44347d
Fix tests.
sborisenkox 9859fa5
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] f209ade
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] ef59642
Merge branch 'crs-tutorials-events-crud' of https://github.com/sboris…
gcf-owl-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
samples/interactive-tutorials/src/main/java/events/PurgeUserEvent.java
This file contains hidden or 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(visitorId, defaultCatalog); | ||
| } | ||
|
|
||
| public static void callPurgeUserEvents(String visitorId, String defaultCatalog) | ||
| 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()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: are we sure we only want to show the user starting this operation, and not waiting for the results?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This operation is running from several hours up to 3 Days, so we will only show the operation Id as a proof, that it was started |
||
| } | ||
| } | ||
| } | ||
|
|
||
| // [END retail_purge_user_event] | ||
77 changes: 77 additions & 0 deletions
77
samples/interactive-tutorials/src/main/java/events/RejoinUserEvent.java
This file contains hidden or 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
samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.