Skip to content

Commit

Permalink
chore(samples): Retail Tutorials. Events (write, rejoin, purge) (#303)
Browse files Browse the repository at this point in the history
* 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
3 people authored Mar 16, 2022
1 parent 2065b6e commit 4a421b0
Show file tree
Hide file tree
Showing 15 changed files with 583 additions and 16 deletions.
20 changes: 19 additions & 1 deletion retail/interactive-tutorials/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>24.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
Expand Down Expand Up @@ -52,6 +64,12 @@
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -61,7 +79,7 @@
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
<cleanupDaemonThreads>true</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
Expand Down
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]
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]
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]
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public static void main(String[] args) throws IOException, InterruptedException
String generatedProductId = UUID.randomUUID().toString();
String productName =
String.format(
"projects/%s/locations/global/catalogs/default_catalog/branches/"
+ "default_branch/products/%s",
"projects/%s/locations/global/catalogs/default_catalog/branches/" + "0/products/%s",
projectId, generatedProductId);
Timestamp currentDate =
Timestamp.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public class ImportProductsBigQueryTable {
private static final String PROJECT_ID = System.getenv("PROJECT_ID");
private static final String DEFAULT_CATALOG =
String.format(
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch",
PROJECT_ID);
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID);
private static final String DATASET_ID = "products";
private static final String TABLE_ID = "products";
// TO CHECK ERROR HANDLING USE THE TABLE WITH INVALID PRODUCTS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public class ImportProductsGcs {
private static final String PROJECT_ID = System.getenv("PROJECT_ID");
private static final String DEFAULT_CATALOG =
String.format(
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch",
PROJECT_ID);
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID);
private static final String GCS_BUCKET = String.format("gs://%s", System.getenv("BUCKET_NAME"));
private static final String GCS_ERROR_BUCKET = String.format("%s/errors", GCS_BUCKET);
private static final String GCS_PRODUCTS_OBJECT = "products.json";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public class ImportProductsInlineSource {
private static final String PROJECT_ID = System.getenv("PROJECT_ID");
private static final String DEFAULT_CATALOG =
String.format(
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch",
PROJECT_ID);
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID);

public static void main(String[] args) throws IOException, InterruptedException {
ImportProductsRequest importRequest = getImportProductsInlineRequest(getProducts());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public static void main(String[] args) throws IOException, InterruptedException
String generatedProductId = UUID.randomUUID().toString();
String productName =
String.format(
"projects/%s/locations/global/catalogs/default_catalog/branches/"
+ "default_branch/products/%s",
"projects/%s/locations/global/catalogs/default_catalog/branches/" + "0/products/%s",
projectId, generatedProductId);
Timestamp currentDate =
Timestamp.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public static void main(String[] args) throws IOException, InterruptedException
String generatedProductId = UUID.randomUUID().toString();
String productName =
String.format(
"projects/%s/locations/global/catalogs/default_catalog/"
+ "branches/default_branch/products/%s",
"projects/%s/locations/global/catalogs/default_catalog/" + "branches/0/products/%s",
projectId, generatedProductId);

createProduct(generatedProductId);
Expand Down
Loading

0 comments on commit 4a421b0

Please sign in to comment.