If your data is partitioned (e.g., by country code), you can compute metrics for the whole table by aggregating the states of individual partitions. This is highly efficient when a single partition changes, as you only need to recompute the state for that specific partition and then re-aggregate.
Compute Partition States: Run AnalysisRunner.run for each partition, saving the state of each using a StateProvider via saveStatesWith.
Aggregate for Whole Table: Use AnalysisRunner.runOnAggregatedStates to compute the final metrics for the entire table by passing the schema, the analysis, and a sequence of the partition StateProviders.
Handle Updates: If a partition changes, re-run the analysis on only that partition to get a new StateProvider, then call runOnAggregatedStates again with the updated sequence of states.
// 1. Compute and save states for each partition
val deStates = InMemoryStateProvider()
val usStates = InMemoryStateProvider()
val cnStates = InMemoryStateProvider()
AnalysisRunner.run(deManufacturers, analysis, saveStatesWith = Some(deStates))
AnalysisRunner.run(usManufacturers, analysis, saveStatesWith = Some(usStates))
AnalysisRunner.run(cnManufacturers, analysis, saveStatesWith = Some(cnStates))
// 2. Aggregate states to get metrics for the whole table
val tableMetrics = AnalysisRunner.runOnAggregatedStates(
deManufacturers.schema,
analysis,
Seq(deStates, usStates, cnStates)
)
// 3. Update only one partition and re-aggregate
val updatedUsStates = InMemoryStateProvider()
AnalysisRunner.run(updatedUsManufacturers, analysis, saveStatesWith = Some(updatedUsStates))
val updatedTableMetrics = AnalysisRunner.runOnAggregatedStates(
deManufacturers.schema,
analysis,
Seq(deStates, updatedUsStates, cnStates)
)