use of com.google.cloud.retail.v2.ImportProductsRequest in project java-retail by googleapis.
the class CreateTestResources method getImportProductsGcsRequest.
public static ImportProductsRequest getImportProductsGcsRequest(String gcsObjectName) {
GcsSource gcsSource = GcsSource.newBuilder().addAllInputUris(Collections.singleton(String.format("gs://%s/%s", BUCKET_NAME, gcsObjectName))).build();
ProductInputConfig inputConfig = ProductInputConfig.newBuilder().setGcsSource(gcsSource).build();
System.out.println("GRS source: " + gcsSource.getInputUrisList());
ImportErrorsConfig errorsConfig = ImportErrorsConfig.newBuilder().setGcsPrefix(GCS_ERROR_BUCKET).build();
ImportProductsRequest importRequest = ImportProductsRequest.newBuilder().setParent(DEFAULT_CATALOG).setReconciliationMode(ReconciliationMode.INCREMENTAL).setInputConfig(inputConfig).setErrorsConfig(errorsConfig).build();
System.out.println("Import products from google cloud source request: " + importRequest);
return importRequest;
}
use of com.google.cloud.retail.v2.ImportProductsRequest in project java-retail by googleapis.
the class CreateTestResources method importProductsFromGcs.
public static void importProductsFromGcs() throws IOException, InterruptedException {
ImportProductsRequest importGcsRequest = getImportProductsGcsRequest("products.json");
try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
String operationName = serviceClient.importProductsCallable().call(importGcsRequest).getName();
System.out.printf("OperationName = %s\n", operationName);
OperationsClient operationsClient = serviceClient.getOperationsClient();
Operation operation = operationsClient.getOperation(operationName);
while (!operation.getDone()) {
System.out.println("Please wait till operation is completed.");
// Keep polling the operation periodically until the import task is done.
int awaitDuration = 30000;
Thread.sleep(awaitDuration);
operation = operationsClient.getOperation(operationName);
}
System.out.println("Import products operation is completed.");
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", response);
}
}
}
use of com.google.cloud.retail.v2.ImportProductsRequest in project java-retail by googleapis.
the class ImportProductsGcs method main.
// TO CHECK ERROR HANDLING USE THE JSON WITH INVALID PRODUCT
// GCS_PRODUCTS_OBJECT = "products_some_invalid.json"
public static void main(String[] args) throws IOException, InterruptedException {
ImportProductsRequest importGcsRequest = getImportProductsGcsRequest(GCS_PRODUCTS_OBJECT);
waitForOperationCompletion(importGcsRequest);
}
use of com.google.cloud.retail.v2.ImportProductsRequest in project java-retail by googleapis.
the class ImportProductsGcs method waitForOperationCompletion.
private 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.
int awaitDuration = 30000;
Thread.sleep(awaitDuration);
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);
}
}
}
use of com.google.cloud.retail.v2.ImportProductsRequest in project java-retail by googleapis.
the class ImportProductsInlineSource method getImportProductsInlineRequest.
public static ImportProductsRequest getImportProductsInlineRequest(List<Product> productsToImport) {
ProductInlineSource inlineSource = ProductInlineSource.newBuilder().addAllProducts(productsToImport).build();
ProductInputConfig inputConfig = ProductInputConfig.newBuilder().setProductInlineSource(inlineSource).build();
ImportProductsRequest importRequest = ImportProductsRequest.newBuilder().setParent(DEFAULT_CATALOG).setInputConfig(inputConfig).build();
System.out.printf("Import products from inline source request: %s%n", importRequest);
return importRequest;
}
Aggregations