Search in sources :

Example 1 with ImportProductsResponse

use of com.google.cloud.retail.v2.ImportProductsResponse 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 2 with ImportProductsResponse

use of com.google.cloud.retail.v2.ImportProductsResponse in project java-retail by googleapis.

the class ImportProductsBigQueryTable method waitForOperationCompletion.

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);
        }
    }
}
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 3 with ImportProductsResponse

use of com.google.cloud.retail.v2.ImportProductsResponse in project java-retail by googleapis.

the class ImportProductsGcs method waitForOperationCompletion.

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);
        }
    }
}
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 4 with ImportProductsResponse

use of com.google.cloud.retail.v2.ImportProductsResponse in project java-retail by googleapis.

the class ImportProductsInlineSource method waitForOperationCompletion.

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);
        }
    }
}
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 ImportProductsResponse

use of com.google.cloud.retail.v2.ImportProductsResponse in project java-retail by googleapis.

the class CreateTestResources method importProductsFromGcs.

public static void importProductsFromGcs(String bucketName, String gcsErrorBucket, String branchName) throws IOException, InterruptedException {
    GcsSource gcsSource = GcsSource.newBuilder().addAllInputUris(Collections.singleton(String.format("gs://%s/%s", bucketName, "products.json"))).build();
    ProductInputConfig inputConfig = ProductInputConfig.newBuilder().setGcsSource(gcsSource).build();
    System.out.println("GRS source: " + gcsSource.getInputUrisList());
    ImportErrorsConfig errorsConfig = ImportErrorsConfig.newBuilder().setGcsPrefix(gcsErrorBucket).build();
    ImportProductsRequest importRequest = ImportProductsRequest.newBuilder().setParent(branchName).setReconciliationMode(ReconciliationMode.INCREMENTAL).setInputConfig(inputConfig).setErrorsConfig(errorsConfig).build();
    System.out.println("Import products from google cloud source request: " + importRequest);
    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()) {
            System.out.println("Please wait till operation is completed.");
            // Keep polling the operation periodically until the import task is done.
            Thread.sleep(30_000);
            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) GcsSource(com.google.cloud.retail.v2.GcsSource) ProductInputConfig(com.google.cloud.retail.v2.ProductInputConfig) ImportErrorsConfig(com.google.cloud.retail.v2.ImportErrorsConfig) 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)

Aggregations

ImportMetadata (com.google.cloud.retail.v2.ImportMetadata)5 ImportProductsResponse (com.google.cloud.retail.v2.ImportProductsResponse)5 ProductServiceClient (com.google.cloud.retail.v2.ProductServiceClient)5 Operation (com.google.longrunning.Operation)5 OperationsClient (com.google.longrunning.OperationsClient)5 ImportProductsRequest (com.google.cloud.retail.v2.ImportProductsRequest)2 GcsSource (com.google.cloud.retail.v2.GcsSource)1 ImportErrorsConfig (com.google.cloud.retail.v2.ImportErrorsConfig)1 ProductInputConfig (com.google.cloud.retail.v2.ProductInputConfig)1