Result: The following Java code sample shows how you can use the consolidation endpoint API. // Retrieve the target index unit matching the filter criteria
IdCard targetIndex = apis.getStorageApi()
.getFilteredStorages(new ListStorageFilter()
.workspaceId("dw-global-000000-default")
.nameFilter("targetIndexUnit"))
.getCards()
.stream()
.findFirst()
.orElseThrow(() -> new RuntimeException("Target index unit not found"));
// Get data query from a json resource file
InputStream inputStream = Example.class.getResourceAsStream("/queries/data-query.json");
JsonNode dataQueryNode = objectMapper.readTree(inputStream);
// Register the consolidation job for the target index
DataConsolidationSubmit jobSubmit = apis.getIndexUnitApi().consolidate(
targetIndex.getResourceUUID(),
new DataConsolidationRequest()
.outputs(List.of("output")) // Specify output options
.query(new DataQueryWrapper().value(dataQueryNode)) // Query details
.request(new QueryExecutionRequest()
.addParametersItem(new ExpressionRequestParameter()
.expression("*") // Expression to execute
.dataQueryParameterName("param")))); // Query parameter
// Get the job ID from the submission response
String jobId = jobSubmit.getJobId();
boolean isRunning = true;
int backoff = 1000; // Milliseconds to wait between status checks
int sleep = 0;
// Polling loop to check if the job status is not PENDING or RUNNING
while (isRunning) {
sleep += backoff;
Thread.sleep(sleep); // Sleep for the increasing backoff time
// Fetch the current status of the consolidation job
DataConsolidationStatus status = apis.getIndexUnitApi().getConsolidationStatus(
targetIndex.getResourceUUID(), jobId);
// Job is still running if the status is PENDING or RUNNING
isRunning = status.getStatus() == DataConsolidationStatus.StatusEnum.PENDING ||
status.getStatus() == DataConsolidationStatus.StatusEnum.RUNNING;
}
// Final status check after the job finishes
DataConsolidationStatus finalStatus = apis.getIndexUnitApi().getConsolidationStatus(
targetIndex.getResourceUUID(), jobId);
// Handle the job's final status
switch (finalStatus.getStatus()) {
case PENDING:
// Job is still pending, handle accordingly
log.info("Consolidation job is still pending...");
break;
case RUNNING:
// Job is still running, handle accordingly
log.info("Consolidation job is still running...");
break;
case COMPLETED:
// Job completed successfully
DataConsolidationSuccessReport success = finalStatus.getReport().getSuccess();
log.info("Consolidation successfully ingested {} events", success.getNbEvents());
break;
case FAILED:
// Job failed, handle the failure report
// Depending on the status code, it can be a good idea to perform a retry of the consolidation. For example, if the status code is 5xx.
DataConsolidationFailureReport failure = finalStatus.getReport().getFailure();
log.error("Consolidation failed with status code {} and message {}", failure.getStatusCode(), failure.getMessage());
break;
case CANCELLED:
// Job was cancelled
log.warn("Consolidation job was cancelled");
break;
default:
log.error("Unexpected consolidation job status: {}", finalStatus.getStatus());
}
// Wait for the async ingestion process to complete
sleep = 0;
boolean isIngesting = true;
// Polling loop to check if the ingestion is still pending
while (isIngesting) {
sleep += backoff;
Thread.sleep(sleep); // Sleep for the increasing backoff time
// Check the number of pending actions for the index unit
isIngesting = apis.getIndexUnitApi()
.getIndexUnitPendingActions(targetIndex.getResourceUUID())
.getNbActions() > 0; // Continue if there are pending actions
}
log.info("Ingestion process completed.");
| |||||||||||||