Search in sources :

Example 6 with ImportMetadata

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

the class ImportUserEventsGcs method importUserEventsFromGcs.

public static void importUserEventsFromGcs(String gcsEventsObject, String defaultCatalog) throws IOException, InterruptedException {
    try {
        String gcsBucket = String.format("gs://%s", EventsCreateGcsBucket.getBucketName());
        String gcsErrorsBucket = String.format("%s/error", gcsBucket);
        GcsSource gcsSource = GcsSource.newBuilder().addInputUris(String.format("%s/%s", gcsBucket, gcsEventsObject)).build();
        UserEventInputConfig inputConfig = UserEventInputConfig.newBuilder().setGcsSource(gcsSource).build();
        ImportErrorsConfig errorsConfig = ImportErrorsConfig.newBuilder().setGcsPrefix(gcsErrorsBucket).build();
        ImportUserEventsRequest importRequest = ImportUserEventsRequest.newBuilder().setParent(defaultCatalog).setInputConfig(inputConfig).setErrorsConfig(errorsConfig).build();
        System.out.printf("Import user events from google cloud source request: %s%n", importRequest);
        // the "close" method on the client to safely clean up any remaining background resources.
        try (UserEventServiceClient serviceClient = UserEventServiceClient.create()) {
            String operationName = serviceClient.importUserEventsCallable().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 events: %s\n", metadata.getSuccessCount());
                System.out.printf("Number of failures during the importing: %s\n", metadata.getFailureCount());
            }
            if (operation.hasResponse()) {
                ImportUserEventsResponse response = operation.getResponse().unpack(ImportUserEventsResponse.class);
                System.out.printf("Operation result: %s%n", response);
            }
        } catch (InvalidArgumentException e) {
            System.out.printf("Given GCS input path was not found. %n%s%n " + "Please run CreateTestResources class to create resources.", e.getMessage());
        }
    } catch (BigQueryException e) {
        System.out.printf("Exception message: %s", e.getMessage());
    }
}
Also used : ImportUserEventsResponse(com.google.cloud.retail.v2.ImportUserEventsResponse) GcsSource(com.google.cloud.retail.v2.GcsSource) ImportErrorsConfig(com.google.cloud.retail.v2.ImportErrorsConfig) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) UserEventInputConfig(com.google.cloud.retail.v2.UserEventInputConfig) UserEventServiceClient(com.google.cloud.retail.v2.UserEventServiceClient) ImportUserEventsRequest(com.google.cloud.retail.v2.ImportUserEventsRequest) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) BigQueryException(com.google.cloud.bigquery.BigQueryException) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata)

Example 7 with ImportMetadata

use of com.google.cloud.retail.v2.ImportMetadata 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)

Example 8 with ImportMetadata

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

the class ImportUserEventsBigQuery method importUserEventsFromBigQuery.

public static void importUserEventsFromBigQuery(String projectId, String defaultCatalog, String datasetId, String tableId) throws IOException, InterruptedException {
    try {
        String dataSchema = "user_event";
        BigQuerySource bigQuerySource = BigQuerySource.newBuilder().setProjectId(projectId).setDatasetId(datasetId).setTableId(tableId).setDataSchema(dataSchema).build();
        UserEventInputConfig inputConfig = UserEventInputConfig.newBuilder().setBigQuerySource(bigQuerySource).build();
        ImportUserEventsRequest importRequest = ImportUserEventsRequest.newBuilder().setParent(defaultCatalog).setInputConfig(inputConfig).build();
        System.out.printf("Import user events from BigQuery source request: %s%n", importRequest);
        // the "close" method on the client to safely clean up any remaining background resources.
        try (UserEventServiceClient serviceClient = UserEventServiceClient.create()) {
            String operationName = serviceClient.importUserEventsCallable().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 events: %s\n", metadata.getSuccessCount());
                System.out.printf("Number of failures during the importing: %s\n", metadata.getFailureCount());
            }
            if (operation.hasResponse()) {
                ImportUserEventsResponse response = operation.getResponse().unpack(ImportUserEventsResponse.class);
                System.out.printf("Operation result: %s%n", response);
            }
        }
    } catch (BigQueryException e) {
        System.out.printf("Exception message: %s", e.getMessage());
    }
}
Also used : ImportUserEventsResponse(com.google.cloud.retail.v2.ImportUserEventsResponse) ImportUserEventsRequest(com.google.cloud.retail.v2.ImportUserEventsRequest) OperationsClient(com.google.longrunning.OperationsClient) Operation(com.google.longrunning.Operation) BigQueryException(com.google.cloud.bigquery.BigQueryException) BigQuerySource(com.google.cloud.retail.v2.BigQuerySource) ImportMetadata(com.google.cloud.retail.v2.ImportMetadata) UserEventInputConfig(com.google.cloud.retail.v2.UserEventInputConfig) UserEventServiceClient(com.google.cloud.retail.v2.UserEventServiceClient)

Aggregations

ImportMetadata (com.google.cloud.retail.v2.ImportMetadata)8 Operation (com.google.longrunning.Operation)7 OperationsClient (com.google.longrunning.OperationsClient)7 ImportProductsResponse (com.google.cloud.retail.v2.ImportProductsResponse)5 ProductServiceClient (com.google.cloud.retail.v2.ProductServiceClient)5 BigQueryException (com.google.cloud.bigquery.BigQueryException)3 ImportUserEventsRequest (com.google.cloud.retail.v2.ImportUserEventsRequest)3 ImportUserEventsResponse (com.google.cloud.retail.v2.ImportUserEventsResponse)3 UserEventInputConfig (com.google.cloud.retail.v2.UserEventInputConfig)3 UserEventServiceClient (com.google.cloud.retail.v2.UserEventServiceClient)3 GcsSource (com.google.cloud.retail.v2.GcsSource)2 ImportErrorsConfig (com.google.cloud.retail.v2.ImportErrorsConfig)2 ImportProductsRequest (com.google.cloud.retail.v2.ImportProductsRequest)2 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 BigQuerySource (com.google.cloud.retail.v2.BigQuerySource)1 ProductInputConfig (com.google.cloud.retail.v2.ProductInputConfig)1 UserEvent (com.google.cloud.retail.v2.UserEvent)1 UserEventInlineSource (com.google.cloud.retail.v2.UserEventInlineSource)1 Timestamp (com.google.protobuf.Timestamp)1 Instant (java.time.Instant)1