Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ If you are using Maven, add this to your pom.xml file:
If you are using Gradle without BOM, add this to your dependencies

```Groovy
implementation 'com.google.cloud:google-cloud-retail:2.0.12'
implementation 'com.google.cloud:google-cloud-retail:2.0.13'
```

If you are using SBT, add this to your dependencies

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.0.12"
libraryDependencies += "com.google.cloud" % "google-cloud-retail" % "2.0.13"
```

## Authentication
Expand Down Expand Up @@ -77,6 +77,9 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-retail/tree/m

| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Purge User Event | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/PurgeUserEvent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/PurgeUserEvent.java) |
| Rejoin User Event | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/RejoinUserEvent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/RejoinUserEvent.java) |
| Write User Event | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/WriteUserEvent.java) |
| Events Create Big Query Table | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/setup/EventsCreateBigQueryTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/setup/EventsCreateBigQueryTable.java) |
| Events Create Gcs Bucket | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/events/setup/EventsCreateGcsBucket.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/events/setup/EventsCreateGcsBucket.java) |
| Create Test Resources | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/init/CreateTestResources.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-retail&page=editor&open_in_editor=samples/interactive-tutorials/src/main/java/init/CreateTestResources.java) |
Expand Down
14 changes: 13 additions & 1 deletion samples/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 @@ -61,7 +73,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(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());

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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]
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