Search in sources :

Example 6 with ImportProductsRequest

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

the class ImportProductsBigQueryTable method getImportProductsBigQueryRequest.

public static ImportProductsRequest getImportProductsBigQueryRequest(ReconciliationMode reconciliationMode, String projectId, String datasetId, String tableId, String dataSchema, String branchName) {
    BigQuerySource bigQuerySource = BigQuerySource.newBuilder().setProjectId(projectId).setDatasetId(datasetId).setTableId(tableId).setDataSchema(dataSchema).build();
    ProductInputConfig inputConfig = ProductInputConfig.newBuilder().setBigQuerySource(bigQuerySource).build();
    ImportProductsRequest importRequest = ImportProductsRequest.newBuilder().setParent(branchName).setReconciliationMode(reconciliationMode).setInputConfig(inputConfig).build();
    System.out.printf("Import products from big query table request: %s%n", importRequest);
    return importRequest;
}
Also used : ImportProductsRequest(com.google.cloud.retail.v2.ImportProductsRequest) ProductInputConfig(com.google.cloud.retail.v2.ProductInputConfig) BigQuerySource(com.google.cloud.retail.v2.BigQuerySource)

Example 7 with ImportProductsRequest

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

the class ImportProductsGcs method main.

public static void main(String[] args) throws IOException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = ServiceOptions.getDefaultProjectId();
    String branchName = String.format("projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId);
    String gcsBucket = String.format("gs://%s", System.getenv("BUCKET_NAME"));
    String gcsErrorBucket = String.format("%s/errors", gcsBucket);
    String gscProductsObject = "products.json";
    // TO CHECK ERROR HANDLING USE THE JSON WITH INVALID PRODUCT
    // GCS_PRODUCTS_OBJECT = "products_some_invalid.json"
    ImportProductsRequest importGcsRequest = getImportProductsGcsRequest(gscProductsObject, gcsBucket, gcsErrorBucket, branchName);
    waitForOperationCompletion(importGcsRequest);
}
Also used : ImportProductsRequest(com.google.cloud.retail.v2.ImportProductsRequest)

Example 8 with ImportProductsRequest

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

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

the class ImportProductsInlineSource method main.

public static void main(String[] args) throws IOException, InterruptedException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = ServiceOptions.getDefaultProjectId();
    String branchName = String.format("projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId);
    ImportProductsRequest importRequest = getImportProductsInlineRequest(getProducts(), branchName);
    waitForOperationCompletion(importRequest);
}
Also used : ImportProductsRequest(com.google.cloud.retail.v2.ImportProductsRequest)

Example 10 with ImportProductsRequest

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

the class ImportProductsBigQueryTableTest method setUp.

@Before
public void setUp() throws IOException, InterruptedException, ExecutionException {
    String projectId = ServiceOptions.getDefaultProjectId();
    String branchName = String.format("projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId);
    String datasetId = "products";
    String tableId = "products";
    String dataSchema = "product";
    ReconciliationMode reconciliationMode = ReconciliationMode.INCREMENTAL;
    bout = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(bout);
    originalPrintStream = System.out;
    System.setOut(out);
    ProductsCreateBigqueryTable.main();
    ImportProductsRequest importBigQueryRequest = getImportProductsBigQueryRequest(reconciliationMode, projectId, datasetId, tableId, dataSchema, branchName);
    waitForOperationCompletion(importBigQueryRequest);
}
Also used : ImportProductsRequest(com.google.cloud.retail.v2.ImportProductsRequest) PrintStream(java.io.PrintStream) ReconciliationMode(com.google.cloud.retail.v2.ImportProductsRequest.ReconciliationMode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Before(org.junit.Before)

Aggregations

ImportProductsRequest (com.google.cloud.retail.v2.ImportProductsRequest)14 ProductInputConfig (com.google.cloud.retail.v2.ProductInputConfig)7 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 GcsSource (com.google.cloud.retail.v2.GcsSource)3 ImportErrorsConfig (com.google.cloud.retail.v2.ImportErrorsConfig)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PrintStream (java.io.PrintStream)3 Before (org.junit.Before)3 BigQuerySource (com.google.cloud.retail.v2.BigQuerySource)2 ReconciliationMode (com.google.cloud.retail.v2.ImportProductsRequest.ReconciliationMode)2 ProductInlineSource (com.google.cloud.retail.v2.ProductInlineSource)2