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