Search in sources :

Example 1 with ImportProductsRequest

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;
}
Also used : ImportProductsRequest(com.google.cloud.retail.v2.ImportProductsRequest) GcsSource(com.google.cloud.retail.v2.GcsSource) ProductInputConfig(com.google.cloud.retail.v2.ProductInputConfig) ImportErrorsConfig(com.google.cloud.retail.v2.ImportErrorsConfig)

Example 2 with ImportProductsRequest

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);
        }
    }
}
Also used : ImportProductsRequest(com.google.cloud.retail.v2.ImportProductsRequest) ImportProductsResponse(com.google.cloud.retail.v2.ImportProductsResponse) ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata)

Example 3 with ImportProductsRequest

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);
}
Also used : ImportProductsRequest(com.google.cloud.retail.v2.ImportProductsRequest)

Example 4 with ImportProductsRequest

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);
        }
    }
}
Also used : ImportProductsResponse(com.google.cloud.retail.v2.ImportProductsResponse) ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata)

Example 5 with ImportProductsRequest

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;
}
Also used : ImportProductsRequest(com.google.cloud.retail.v2.ImportProductsRequest) ProductInputConfig(com.google.cloud.retail.v2.ProductInputConfig) ProductInlineSource(com.google.cloud.retail.v2.ProductInlineSource)

Aggregations

ImportProductsRequest (com.google.cloud.retail.v2.ImportProductsRequest)8 ImportMetadata (com.google.cloud.retail.v2.ImportMetadata)4 ImportProductsResponse (com.google.cloud.retail.v2.ImportProductsResponse)4 ProductInputConfig (com.google.cloud.retail.v2.ProductInputConfig)4 ProductServiceClient (com.google.cloud.retail.v2.ProductServiceClient)4 Operation (com.google.longrunning.Operation)4 OperationsClient (com.google.longrunning.OperationsClient)4 GcsSource (com.google.cloud.retail.v2.GcsSource)2 ImportErrorsConfig (com.google.cloud.retail.v2.ImportErrorsConfig)2 BigQuerySource (com.google.cloud.retail.v2.BigQuerySource)1 ReconciliationMode (com.google.cloud.retail.v2.ImportProductsRequest.ReconciliationMode)1 ProductInlineSource (com.google.cloud.retail.v2.ProductInlineSource)1