Skip to content

Commit

Permalink
Add search samples impl
Browse files Browse the repository at this point in the history
Minor fixes

added product and events services

fixes according to the review comments
  • Loading branch information
t-karasova authored and Tatiana Karasyova committed Jan 14, 2022
1 parent bc15da9 commit 434853a
Show file tree
Hide file tree
Showing 72 changed files with 6,110 additions and 222 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -106,6 +106,7 @@
<module>grpc-google-cloud-retail-v2</module>
<module>proto-google-cloud-retail-v2</module>
<module>google-cloud-retail-bom</module>
<module>samples</module>
</modules>

<reporting>
Expand Down
41 changes: 3 additions & 38 deletions samples/install-without-bom/pom.xml
Expand Up @@ -12,9 +12,9 @@
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-retail-samples</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<properties>
Expand Down Expand Up @@ -46,39 +46,4 @@
<scope>test</scope>
</dependency>
</dependencies>

<!-- compile and run all snippet tests -->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-snippets-source</id>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>../snippets/src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-snippets-tests</id>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>../snippets/src/test/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
6 changes: 3 additions & 3 deletions samples/pom.xml
Expand Up @@ -16,9 +16,9 @@
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-retail-parent</artifactId>
<version>2.0.8-SNAPSHOT</version>
</parent>

<properties>
Expand Down
41 changes: 3 additions & 38 deletions samples/snapshot/pom.xml
Expand Up @@ -12,9 +12,9 @@
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-retail-samples</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<properties>
Expand Down Expand Up @@ -45,39 +45,4 @@
<scope>test</scope>
</dependency>
</dependencies>

<!-- compile and run all snippet tests -->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-snippets-source</id>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>../snippets/src/main/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-snippets-tests</id>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>../snippets/src/test/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
50 changes: 34 additions & 16 deletions samples/snippets/pom.xml
@@ -1,7 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud</groupId>
<artifactId>retail-snippets</artifactId>
Expand All @@ -14,19 +12,30 @@
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-retail-samples</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<groupId>java_grs_samples</groupId>
<artifactId>java_grs_samples</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
Expand All @@ -40,10 +49,19 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>
</project>
</project>
182 changes: 182 additions & 0 deletions samples/snippets/src/main/java/events/ImportUserEventsBigQuery.java
@@ -0,0 +1,182 @@
/*
* Copyright 2022 Google Inc.
*
* 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_import_user_events_from_big_query]
* Import user events into a catalog from GCS using Retail API
*/


package events;

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.retail.v2.BigQuerySource;
import com.google.cloud.retail.v2.ImportMetadata;
import com.google.cloud.retail.v2.ImportUserEventsRequest;
import com.google.cloud.retail.v2.ImportUserEventsResponse;
import com.google.cloud.retail.v2.UserEventInputConfig;
import com.google.cloud.retail.v2.UserEventServiceClient;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public final class ImportUserEventsBigQuery {

/**
* This variable describes project number getting from environment variable.
*/
private static final String PROJECT_NUMBER = System.getenv("PROJECT_NUMBER");

/**
* This variable describes project id getting from environment variable.
*/
private static final String PROJECT_ID = System.getenv("PROJECT_ID");

/**
* This variable describes default catalog name.
*/
private static final String DEFAULT_CATALOG = String.format(
"projects/%s/locations/global/catalogs/default_catalog",
PROJECT_NUMBER);

/**
* This variable describes dataset id.
*/
private static final String DATASET_ID = "user_events";

/**
* This variable describes table id.
*/
private static final String TABLE_ID = "events";

/**
* This variable describes data schema.
*/
private static final String DATA_SCHEMA = "user_event";

/*
TO CHECK ERROR HANDLING USE THE TABLE OF INVALID USER EVENTS:
TABLE_ID = "events_some_invalid"
*/

private ImportUserEventsBigQuery() {
}

/**
* Get user event service client.
*
* @return UserEventServiceClient.
* @throws IOException if endpoint is incorrect.
*/
private static UserEventServiceClient getUserEventsServiceClient()
throws IOException {
return UserEventServiceClient.create();
}

/**
* Get import user events from big query request.
*
* @return ImportUserEventsRequest.
*/
public static ImportUserEventsRequest getImportEventsBigQueryRequest() {
// TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE:
// DEFAULT_CATALOG = "invalid_catalog_name"

BigQuerySource bigQuerySource = BigQuerySource.newBuilder()
.setProjectId(PROJECT_ID)
.setDatasetId(DATASET_ID)
.setTableId(TABLE_ID)
.setDataSchema(DATA_SCHEMA)
.build();

UserEventInputConfig inputConfig = UserEventInputConfig.newBuilder()
.setBigQuerySource(bigQuerySource)
.build();

ImportUserEventsRequest importRequest = ImportUserEventsRequest.newBuilder()
.setParent(DEFAULT_CATALOG)
.setInputConfig(inputConfig)
.build();

System.out.printf("Import user events from BigQuery source request: %s%n",
importRequest);

return importRequest;
}

/**
* Call the Retail API to import user events.
*
* @throws IOException from the called method.
* @throws ExecutionException when attempting to retrieve the result of a
* task that aborted by throwing an exception.
* @throws InterruptedException when a thread is waiting, sleeping, or
* otherwise occupied, and the thread is
* interrupted, either before or during the
* activity.
*/
public static void importUserEventsFromBigQuery()
throws IOException, ExecutionException, InterruptedException {
ImportUserEventsRequest importBigQueryRequest =
getImportEventsBigQueryRequest();

OperationFuture<ImportUserEventsResponse, ImportMetadata> bigQueryOperation
= getUserEventsServiceClient().importUserEventsAsync(
importBigQueryRequest);

System.out.printf("The operation was started: %s%n",
bigQueryOperation.getName());

while (!bigQueryOperation.isDone()) {
final int awaitDuration = 30;

System.out.println("Please wait till operation is done.");

getUserEventsServiceClient().awaitTermination(
awaitDuration, TimeUnit.SECONDS);

System.out.println("Import user events operation is done.");

if (bigQueryOperation.getMetadata().get() != null) {
System.out.printf("Number of successfully imported events: %s%n",
bigQueryOperation.getMetadata().get().getSuccessCount());

System.out.printf("Number of failures during the importing: %s%n",
bigQueryOperation.getMetadata().get().getFailureCount());
} else {
System.out.println("Metadata in bigQuery operation is empty.");
}
if (bigQueryOperation.get() != null) {
System.out.printf("Operation result: %s%n", bigQueryOperation.get());
} else {
System.out.println("Operation result is empty.");
}
}
}

/**
* Executable tutorial class.
*
* @param args command line arguments.
*/
public static void main(final String[] args)
throws IOException, ExecutionException, InterruptedException {
importUserEventsFromBigQuery();
}
}

// [END retail_import_user_events_from_big_query]

0 comments on commit 434853a

Please sign in to comment.