|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// [START retail_import_products_from_gcs] |
| 18 | + |
| 19 | +/* |
| 20 | + * Import products into a catalog from gcs using Retail API |
| 21 | + */ |
| 22 | + |
| 23 | +package product; |
| 24 | + |
| 25 | +import com.google.cloud.retail.v2.GcsSource; |
| 26 | +import com.google.cloud.retail.v2.ImportErrorsConfig; |
| 27 | +import com.google.cloud.retail.v2.ImportMetadata; |
| 28 | +import com.google.cloud.retail.v2.ImportProductsRequest; |
| 29 | +import com.google.cloud.retail.v2.ImportProductsRequest.ReconciliationMode; |
| 30 | +import com.google.cloud.retail.v2.ImportProductsResponse; |
| 31 | +import com.google.cloud.retail.v2.ProductInputConfig; |
| 32 | +import com.google.cloud.retail.v2.ProductServiceClient; |
| 33 | +import com.google.longrunning.Operation; |
| 34 | +import com.google.longrunning.OperationsClient; |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.Collections; |
| 37 | + |
| 38 | +public class ImportProductsGcs { |
| 39 | + |
| 40 | + private static final String PROJECT_ID = System.getenv("PROJECT_ID"); |
| 41 | + private static final String DEFAULT_CATALOG = |
| 42 | + String.format( |
| 43 | + "projects/%s/locations/global/catalogs/default_catalog/" + "branches/default_branch", |
| 44 | + PROJECT_ID); |
| 45 | + private static final String GCS_BUCKET = String.format("gs://%s", System.getenv("BUCKET_NAME")); |
| 46 | + private static final String GCS_ERROR_BUCKET = String.format("%s/errors", GCS_BUCKET); |
| 47 | + private static final String GCS_PRODUCTS_OBJECT = "products.json"; |
| 48 | + // TO CHECK ERROR HANDLING USE THE JSON WITH INVALID PRODUCT |
| 49 | + // GCS_PRODUCTS_OBJECT = "products_some_invalid.json" |
| 50 | + |
| 51 | + public static void main(String[] args) throws IOException, InterruptedException { |
| 52 | + ImportProductsRequest importGcsRequest = getImportProductsGcsRequest(GCS_PRODUCTS_OBJECT); |
| 53 | + waitForOperationCompletion(importGcsRequest); |
| 54 | + } |
| 55 | + |
| 56 | + public static ImportProductsRequest getImportProductsGcsRequest(String gcsObjectName) { |
| 57 | + GcsSource gcsSource = |
| 58 | + GcsSource.newBuilder() |
| 59 | + .addAllInputUris( |
| 60 | + Collections.singleton(String.format("%s/%s", GCS_BUCKET, gcsObjectName))) |
| 61 | + .build(); |
| 62 | + |
| 63 | + ProductInputConfig inputConfig = |
| 64 | + ProductInputConfig.newBuilder().setGcsSource(gcsSource).build(); |
| 65 | + |
| 66 | + System.out.println("GRS source: " + gcsSource.getInputUrisList()); |
| 67 | + |
| 68 | + ImportErrorsConfig errorsConfig = |
| 69 | + ImportErrorsConfig.newBuilder().setGcsPrefix(GCS_ERROR_BUCKET).build(); |
| 70 | + |
| 71 | + ImportProductsRequest importRequest = |
| 72 | + ImportProductsRequest.newBuilder() |
| 73 | + .setParent(DEFAULT_CATALOG) |
| 74 | + .setReconciliationMode(ReconciliationMode.INCREMENTAL) |
| 75 | + .setInputConfig(inputConfig) |
| 76 | + .setErrorsConfig(errorsConfig) |
| 77 | + .build(); |
| 78 | + |
| 79 | + System.out.println("Import products from google cloud source request: " + importRequest); |
| 80 | + |
| 81 | + return importRequest; |
| 82 | + } |
| 83 | + |
| 84 | + private static void waitForOperationCompletion(ImportProductsRequest importRequest) |
| 85 | + throws IOException, InterruptedException { |
| 86 | + try (ProductServiceClient serviceClient = ProductServiceClient.create()) { |
| 87 | + String operationName = serviceClient.importProductsCallable().call(importRequest).getName(); |
| 88 | + System.out.printf("OperationName = %s\n", operationName); |
| 89 | + |
| 90 | + OperationsClient operationsClient = serviceClient.getOperationsClient(); |
| 91 | + Operation operation = operationsClient.getOperation(operationName); |
| 92 | + |
| 93 | + while (!operation.getDone()) { |
| 94 | + // Keep polling the operation periodically until the import task is done. |
| 95 | + int awaitDuration = 30000; |
| 96 | + Thread.sleep(awaitDuration); |
| 97 | + operation = operationsClient.getOperation(operationName); |
| 98 | + } |
| 99 | + |
| 100 | + if (operation.hasMetadata()) { |
| 101 | + ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class); |
| 102 | + System.out.printf( |
| 103 | + "Number of successfully imported products: %s\n", metadata.getSuccessCount()); |
| 104 | + System.out.printf( |
| 105 | + "Number of failures during the importing: %s\n", metadata.getFailureCount()); |
| 106 | + } |
| 107 | + |
| 108 | + if (operation.hasResponse()) { |
| 109 | + ImportProductsResponse response = |
| 110 | + operation.getResponse().unpack(ImportProductsResponse.class); |
| 111 | + System.out.printf("Operation result: %s%n", response); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// [END retail_import_products_from_gcs] |
0 commit comments