Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
docs(samples): Refactoring product package (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfirova authored Sep 13, 2022
1 parent 20d2539 commit 0c39897
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 132 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-retail/tree/m
| Update Product | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/UpdateProduct.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/product/UpdateProduct.java) |
| Products Create Bigquery Table | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateBigqueryTable.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/product/setup/ProductsCreateBigqueryTable.java) |
| Products Create Gcs Bucket | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/setup/ProductsCreateGcsBucket.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/product/setup/ProductsCreateGcsBucket.java) |
| Remove Products Resources | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/product/setup/RemoveProductsResources.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/product/setup/RemoveProductsResources.java) |
| Search Simple Query | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/search/SearchSimpleQuery.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/search/SearchSimpleQuery.java) |
| Search With Boost Spec | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/search/SearchWithBoostSpec.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/search/SearchWithBoostSpec.java) |
| Search With Facet Spec | [source code](https://github.com/googleapis/java-retail/blob/main/samples/interactive-tutorials/src/main/java/search/SearchWithFacetSpec.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/search/SearchWithFacetSpec.java) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,30 @@
import com.google.longrunning.Operation;
import com.google.longrunning.OperationsClient;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class ImportProductsBigQueryTable {

public static void main(String[] args) throws IOException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = ServiceOptions.getDefaultProjectId();
String branchName =
String.format(
"projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId);
String datasetId = "products";
String tableId = "products";
// TO CHECK ERROR HANDLING USE THE TABLE WITH INVALID PRODUCTS:
// TABLE_ID = "products_some_invalid"
String dataSchema = "product";
// TRY THE FULL RECONCILIATION MODE HERE:
ReconciliationMode reconciliationMode = ReconciliationMode.INCREMENTAL;
// tableId = "products_some_invalid"

ImportProductsRequest importBigQueryRequest =
getImportProductsBigQueryRequest(
reconciliationMode, projectId, datasetId, tableId, dataSchema, branchName);
waitForOperationCompletion(importBigQueryRequest);
importProductsFromBigQuery(projectId, branchName, datasetId, tableId);
}

public static ImportProductsRequest getImportProductsBigQueryRequest(
ReconciliationMode reconciliationMode,
String projectId,
String datasetId,
String tableId,
String dataSchema,
String branchName) {
public static void importProductsFromBigQuery(
String projectId, String branchName, String datasetId, String tableId)
throws IOException, InterruptedException {
// TRY THE FULL RECONCILIATION MODE HERE:
ReconciliationMode reconciliationMode = ReconciliationMode.INCREMENTAL;
String dataSchema = "product";

BigQuerySource bigQuerySource =
BigQuerySource.newBuilder()
.setProjectId(projectId)
Expand All @@ -80,30 +74,29 @@ public static ImportProductsRequest getImportProductsBigQueryRequest(
.build();
System.out.printf("Import products from big query table request: %s%n", importRequest);

return importRequest;
}

public static void waitForOperationCompletion(ImportProductsRequest importRequest)
throws IOException, InterruptedException {
// 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 (ProductServiceClient serviceClient = ProductServiceClient.create()) {
String operationName = serviceClient.importProductsCallable().call(importRequest).getName();
System.out.printf("OperationName = %s\n", operationName);
System.out.printf("OperationName = %s%n", operationName);

OperationsClient operationsClient = serviceClient.getOperationsClient();
Operation operation = operationsClient.getOperation(operationName);

while (!operation.getDone()) {
// Keep polling the operation periodically until the import task is done.
Thread.sleep(30_000);
TimeUnit.SECONDS.sleep(30);
operation = operationsClient.getOperation(operationName);
}

if (operation.hasMetadata()) {
ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
System.out.printf(
"Number of successfully imported products: %s\n", metadata.getSuccessCount());
"Number of successfully imported products: %s%n", metadata.getSuccessCount());
System.out.printf(
"Number of failures during the importing: %s\n", metadata.getFailureCount());
"Number of failures during the importing: %s%n", metadata.getFailureCount());
}

if (operation.hasResponse()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package product;

import com.google.api.gax.rpc.InvalidArgumentException;
import com.google.api.gax.rpc.PermissionDeniedException;
import com.google.cloud.ServiceOptions;
import com.google.cloud.retail.v2.GcsSource;
import com.google.cloud.retail.v2.ImportErrorsConfig;
Expand All @@ -32,37 +34,42 @@
import com.google.longrunning.Operation;
import com.google.longrunning.OperationsClient;
import java.io.IOException;
import java.time.Instant;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

public class ImportProductsGcs {

public static void main(String[] args) throws IOException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = ServiceOptions.getDefaultProjectId();
String branchName =
String.format(
"projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId);

String bucketName = System.getenv("BUCKET_NAME");
String gcsBucket = String.format("gs://%s", bucketName);
String gcsErrorBucket = String.format("%s/errors", gcsBucket);

// To check error handling, use an invalid catalog in request
// branchName =
// String.format("projects/%s/locations/global/catalogs/invalid_catalog/branches/default_branch", projectId);

String gcsBucket = String.format("gs://%s", System.getenv("BUCKET_NAME"));
String gcsErrorBucket = String.format("%s/errors", gcsBucket);
String gcsProductsObject = "products.json";
// To check error handling, use an invalid product JSON.
// gcsProductsObject = "products_some_invalid.json";
// gcsProductsObject = "products_some_invalid.json"

ImportProductsRequest importGcsRequest =
getImportProductsGcsRequest(gcsProductsObject, gcsBucket, gcsErrorBucket, branchName);
waitForOperationCompletion(importGcsRequest);
importProductsFromGcs(branchName, gcsBucket, gcsProductsObject);
}

public static ImportProductsRequest getImportProductsGcsRequest(
String gcsObjectName, String gcsBucket, String gcsErrorBucket, String branchName) {
public static void importProductsFromGcs(
String branchName, String gcsBucket, String gcsProductsObject)
throws IOException, InterruptedException {
String gcsErrorBucket = String.format("%s/errors", gcsBucket);

GcsSource gcsSource =
GcsSource.newBuilder()
.addAllInputUris(
Collections.singleton(String.format("%s/%s", gcsBucket, gcsObjectName)))
Collections.singleton(String.format("%s/%s", gcsBucket, gcsProductsObject)))
.build();

ProductInputConfig inputConfig =
Expand All @@ -81,39 +88,53 @@ public static ImportProductsRequest getImportProductsGcsRequest(
.setErrorsConfig(errorsConfig)
.build();

System.out.println("Import products from google cloud source request: " + importRequest);

return importRequest;
}
System.out.printf("Import products from google cloud source request: %s%n", importRequest);

public static void waitForOperationCompletion(ImportProductsRequest importRequest)
throws IOException, InterruptedException {
// 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 (ProductServiceClient serviceClient = ProductServiceClient.create()) {
String operationName = serviceClient.importProductsCallable().call(importRequest).getName();
System.out.printf("OperationName = %s\n", operationName);

System.out.println("The operation was started.");
System.out.printf("OperationName = %s%n", operationName);

OperationsClient operationsClient = serviceClient.getOperationsClient();
Operation operation = operationsClient.getOperation(operationName);

while (!operation.getDone()) {
// Keep polling the operation periodically until the import task is done.
Thread.sleep(30_000);
Instant deadline = Instant.now().plusSeconds(60);

while (!operation.getDone() || Instant.now().isBefore(deadline)) {
System.out.println("Please wait till operation is done.");
TimeUnit.SECONDS.sleep(30);
operation = operationsClient.getOperation(operationName);
}

if (operation.hasMetadata()) {
ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
System.out.printf(
"Number of successfully imported products: %s\n", metadata.getSuccessCount());
"Number of successfully imported products: %s%n", metadata.getSuccessCount());
System.out.printf(
"Number of failures during the importing: %s\n", metadata.getFailureCount());
"Number of failures during the importing: %s%n", metadata.getFailureCount());
} else {
System.out.println("Metadata is empty.");
}

if (operation.hasResponse()) {
ImportProductsResponse response =
operation.getResponse().unpack(ImportProductsResponse.class);
System.out.printf("Operation result: %s%n", response);
} else {
System.out.println("Operation result is empty.");
}
} catch (InvalidArgumentException e) {
System.out.printf(
"%s%n'%s' file does not exist in the bucket. Please "
+ "make sure you have followed the setting up instructions.",
e.getMessage(), gcsProductsObject);
} catch (PermissionDeniedException e) {
System.out.println(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package product;

import com.google.api.gax.rpc.InvalidArgumentException;
import com.google.cloud.ServiceOptions;
import com.google.cloud.retail.v2.ColorInfo;
import com.google.cloud.retail.v2.FulfillmentInfo;
Expand All @@ -40,24 +41,23 @@
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class ImportProductsInlineSource {

public static void main(String[] args) throws IOException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = ServiceOptions.getDefaultProjectId();
String branchName =
String.format(
"projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId);

ImportProductsRequest importRequest = getImportProductsInlineRequest(getProducts(), branchName);
waitForOperationCompletion(importRequest);
importProductsInlineSource(branchName);
}

public static ImportProductsRequest getImportProductsInlineRequest(
List<Product> productsToImport, String branchName) {
public static void importProductsInlineSource(String branchName)
throws IOException, InterruptedException {
ProductInlineSource inlineSource =
ProductInlineSource.newBuilder().addAllProducts(productsToImport).build();
ProductInlineSource.newBuilder().addAllProducts(getProducts()).build();

ProductInputConfig inputConfig =
ProductInputConfig.newBuilder().setProductInlineSource(inlineSource).build();
Expand All @@ -70,7 +70,41 @@ public static ImportProductsRequest getImportProductsInlineRequest(

System.out.printf("Import products from inline source request: %s%n", importRequest);

return importRequest;
// 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 (ProductServiceClient serviceClient = ProductServiceClient.create()) {
String operationName = serviceClient.importProductsCallable().call(importRequest).getName();
System.out.printf("OperationName = %s%n", operationName);

OperationsClient operationsClient = serviceClient.getOperationsClient();
Operation operation = operationsClient.getOperation(operationName);

long assuredBreak = System.currentTimeMillis() + 60000; // 60 seconds delay

while (!operation.getDone() || System.currentTimeMillis() < assuredBreak) {
// Keep polling the operation periodically until the import task is done.
TimeUnit.SECONDS.sleep(30);
operation = operationsClient.getOperation(operationName);
}

if (operation.hasMetadata()) {
ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
System.out.printf(
"Number of successfully imported products: %s%n", metadata.getSuccessCount());
System.out.printf(
"Number of failures during the importing: %s%n", metadata.getFailureCount());
}

if (operation.hasResponse()) {
ImportProductsResponse response =
operation.getResponse().unpack(ImportProductsResponse.class);
System.out.printf("Operation result: %s%n", response);
}
} catch (InvalidArgumentException e) {
System.out.println(e.getMessage());
}
}

public static List<Product> getProducts() {
Expand Down Expand Up @@ -173,35 +207,4 @@ public static List<Product> getProducts() {

return products;
}

public static void waitForOperationCompletion(ImportProductsRequest importRequest)
throws IOException, InterruptedException {
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
String operationName = serviceClient.importProductsCallable().call(importRequest).getName();
System.out.printf("OperationName = %s\n", operationName);

OperationsClient operationsClient = serviceClient.getOperationsClient();
Operation operation = operationsClient.getOperation(operationName);

while (!operation.getDone()) {
// Keep polling the operation periodically until the import task is done.
Thread.sleep(30_000);
operation = operationsClient.getOperation(operationName);
}

if (operation.hasMetadata()) {
ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class);
System.out.printf(
"Number of successfully imported products: %s\n", metadata.getSuccessCount());
System.out.printf(
"Number of failures during the importing: %s\n", metadata.getFailureCount());
}

if (operation.hasResponse()) {
ImportProductsResponse response =
operation.getResponse().unpack(ImportProductsResponse.class);
System.out.printf("Operation result: %s%n", response);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@

public class ProductsCreateBigqueryTable {

public static void main(String... args) throws IOException {
public static void main(String[] args) throws IOException {
String dataset = "products";
String validProductsTable = "products";
String invalidProductsTable = "products_some_invalid";
String productSchemaFilePath = "src/main/resources/product_schema.json";
String validProductsSourceFile =
String.format("gs://%s/products.json", ProductsCreateGcsBucket.getBucketName());
String.format("gs://%s/products.json", System.getenv("BUCKET_NAME"));
String invalidProductsSourceFile =
String.format(
"gs://%s/products_some_invalid.json", ProductsCreateGcsBucket.getBucketName());
String.format("gs://%s/products_some_invalid.json", System.getenv("BUCKET_NAME"));

BufferedReader bufferedReader = new BufferedReader(new FileReader(productSchemaFilePath));
String jsonToString = bufferedReader.lines().collect(Collectors.joining());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ public class ProductsCreateGcsBucket {
String.format("%s_products_%s", PROJECT_ID, CURRENT_DATE.getSeconds());

public static void main(String... args) throws IOException {
createBucket(BUCKET_NAME);
System.out.printf("Products gcs bucket %s was created.", BUCKET_NAME);
createGcsBucketAndUploadData(BUCKET_NAME);
}

public static void createGcsBucketAndUploadData(String bucketName) throws IOException {
createBucket(bucketName);
System.out.printf("Products gcs bucket %s was created.%n", bucketName);

uploadObject(BUCKET_NAME, "products.json", "src/main/resources/products.json");
System.out.printf("File 'products.json' was uploaded into bucket '%s'.", BUCKET_NAME);
uploadObject(bucketName, "products.json", "src/main/resources/products.json");
System.out.printf("File 'products.json' was uploaded into bucket '%s'.%n", bucketName);

uploadObject(
BUCKET_NAME, "products_some_invalid.json", "src/main/resources/products_some_invalid.json");
bucketName, "products_some_invalid.json", "src/main/resources/products_some_invalid.json");
System.out.printf(
"File 'products_some_invalid.json' was uploaded into bucket '%s'.", BUCKET_NAME);
"File 'products_some_invalid.json' was uploaded into bucket '%s'.%n", bucketName);
}

public static String getBucketName() {
Expand Down
Loading

0 comments on commit 0c39897

Please sign in to comment.