Change Data Capture (CDC), is to apply all data changes generated from an external data set into a target dataset. In other words, a set of changes (update/delete/insert) applied to an external table needs to be applied to a target table.
Slowly Changing Dimensions (SCD), are the dimensions in which the data changes slowly, rather than changing regularly on a time basis.
SCD and CDC data changes can be merged to a carbon dataset online using the data frame level
Below API merges the datasets online and applies the actions as per the conditions.
targetDS.merge(sourceDS, <condition>) .whenMatched(<condition>) .updateExpr(updateMap) .insertExpr(insertMap_u) .whenNotMatched(<condition>) .insertExpr(insertMap) .whenNotMatchedAndExistsOnlyOnTarget(<condition>) .delete() .execute()
Below is the detailed description of the
merge API operation.
mergewill merge the datasets based on a condition.
whenMatchedclauses are executed when a source row matches a target table row based on the match condition. These clauses have the following semantics.
whenMatchedclauses can have at most one updateExpr and one delete action. The
updateExpraction in merge only updates the specified columns of the matched target row. The
deleteaction deletes the matched row.
whenMatchedclauses, then they are evaluated in order they are specified. The first clause must have a clause condition (otherwise the second clause is never executed).
whenMatchedclauses have conditions and neither of the conditions are true for a matching source-target row pair, then the matched target row is left unchanged.
whenNotMatchedclause is executed when a source row does not match any target row based on the match condition.
whenNotMatchedclause can have only the
insertExpraction. The new row is generated based on the specified column and corresponding expressions. Users do not need to specify all the columns in the target table. For unspecified target columns, NULL is inserted.
whenNotMatchedAndExistsOnlyOnTargetclause is executed when row does not match source and exists only in target. This clause can have only delete action.
NOTE: SQL syntax for merge is not yet supported.
Please refer example class MergeTestCase to understand and implement scd and cdc scenarios.