|
| 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 | +package init; |
| 18 | + |
| 19 | +import static events.setup.EventsCreateBigQueryTable.createBqTableWithEvents; |
| 20 | +import static events.setup.EventsCreateGcsBucket.eventsCreateGcsBucketAndUploadJsonFiles; |
| 21 | +import static product.setup.ProductsCreateBigqueryTable.createBqTableWithProducts; |
| 22 | +import static product.setup.ProductsCreateGcsBucket.productsCreateGcsBucketAndUploadJsonFiles; |
| 23 | + |
| 24 | +import com.google.cloud.retail.v2.GcsSource; |
| 25 | +import com.google.cloud.retail.v2.ImportErrorsConfig; |
| 26 | +import com.google.cloud.retail.v2.ImportMetadata; |
| 27 | +import com.google.cloud.retail.v2.ImportProductsRequest; |
| 28 | +import com.google.cloud.retail.v2.ImportProductsRequest.ReconciliationMode; |
| 29 | +import com.google.cloud.retail.v2.ImportProductsResponse; |
| 30 | +import com.google.cloud.retail.v2.ProductInputConfig; |
| 31 | +import com.google.cloud.retail.v2.ProductServiceClient; |
| 32 | +import com.google.longrunning.Operation; |
| 33 | +import com.google.longrunning.OperationsClient; |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.Collections; |
| 36 | + |
| 37 | +public class CreateTestResources { |
| 38 | + private static final String PROJECT_ID = System.getenv("PROJECT_ID"); |
| 39 | + private static final String BUCKET_NAME = System.getenv("BUCKET_NAME"); |
| 40 | + private static final String GCS_BUCKET = String.format("gs://%s", System.getenv("BUCKET_NAME")); |
| 41 | + private static final String GCS_ERROR_BUCKET = String.format("%s/errors", GCS_BUCKET); |
| 42 | + private static final String DEFAULT_CATALOG = |
| 43 | + String.format( |
| 44 | + "projects/%s/locations/global/catalogs/default_catalog/" + "branches/0", PROJECT_ID); |
| 45 | + |
| 46 | + public static void main(String[] args) throws IOException, InterruptedException { |
| 47 | + productsCreateGcsBucketAndUploadJsonFiles(); |
| 48 | + eventsCreateGcsBucketAndUploadJsonFiles(); |
| 49 | + importProductsFromGcs(); |
| 50 | + createBqTableWithProducts(); |
| 51 | + createBqTableWithEvents(); |
| 52 | + } |
| 53 | + |
| 54 | + public static ImportProductsRequest getImportProductsGcsRequest(String gcsObjectName) { |
| 55 | + GcsSource gcsSource = |
| 56 | + GcsSource.newBuilder() |
| 57 | + .addAllInputUris( |
| 58 | + Collections.singleton(String.format("gs://%s/%s", BUCKET_NAME, gcsObjectName))) |
| 59 | + .build(); |
| 60 | + ProductInputConfig inputConfig = |
| 61 | + ProductInputConfig.newBuilder().setGcsSource(gcsSource).build(); |
| 62 | + System.out.println("GRS source: " + gcsSource.getInputUrisList()); |
| 63 | + |
| 64 | + ImportErrorsConfig errorsConfig = |
| 65 | + ImportErrorsConfig.newBuilder().setGcsPrefix(GCS_ERROR_BUCKET).build(); |
| 66 | + ImportProductsRequest importRequest = |
| 67 | + ImportProductsRequest.newBuilder() |
| 68 | + .setParent(DEFAULT_CATALOG) |
| 69 | + .setReconciliationMode(ReconciliationMode.INCREMENTAL) |
| 70 | + .setInputConfig(inputConfig) |
| 71 | + .setErrorsConfig(errorsConfig) |
| 72 | + .build(); |
| 73 | + System.out.println("Import products from google cloud source request: " + importRequest); |
| 74 | + |
| 75 | + return importRequest; |
| 76 | + } |
| 77 | + |
| 78 | + public static void importProductsFromGcs() throws IOException, InterruptedException { |
| 79 | + ImportProductsRequest importGcsRequest = getImportProductsGcsRequest("products.json"); |
| 80 | + |
| 81 | + try (ProductServiceClient serviceClient = ProductServiceClient.create()) { |
| 82 | + String operationName = |
| 83 | + serviceClient.importProductsCallable().call(importGcsRequest).getName(); |
| 84 | + System.out.printf("OperationName = %s\n", operationName); |
| 85 | + |
| 86 | + OperationsClient operationsClient = serviceClient.getOperationsClient(); |
| 87 | + Operation operation = operationsClient.getOperation(operationName); |
| 88 | + |
| 89 | + while (!operation.getDone()) { |
| 90 | + System.out.println("Please wait till operation is completed."); |
| 91 | + // Keep polling the operation periodically until the import task is done. |
| 92 | + int awaitDuration = 30000; |
| 93 | + Thread.sleep(awaitDuration); |
| 94 | + operation = operationsClient.getOperation(operationName); |
| 95 | + } |
| 96 | + |
| 97 | + System.out.println("Import products operation is completed."); |
| 98 | + |
| 99 | + if (operation.hasMetadata()) { |
| 100 | + ImportMetadata metadata = operation.getMetadata().unpack(ImportMetadata.class); |
| 101 | + System.out.printf( |
| 102 | + "Number of successfully imported products: %s\n", metadata.getSuccessCount()); |
| 103 | + System.out.printf( |
| 104 | + "Number of failures during the importing: %s\n", metadata.getFailureCount()); |
| 105 | + } |
| 106 | + |
| 107 | + if (operation.hasResponse()) { |
| 108 | + ImportProductsResponse response = |
| 109 | + operation.getResponse().unpack(ImportProductsResponse.class); |
| 110 | + System.out.printf("Operation result: %s", response); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments