Conditionally map Data from Source to Target
Updated: Jun 20, 2023
This example shows how you can use Data Pipeline to perform data mapping operations from source to target with some conditions. It allows users to define conditional rules that determine how data should be mapped based on specific criteria, enabling flexible and dynamic data transformation.
The example can be used to selectively map data based on certain conditions, allowing users to filter and segment data during the mapping process. This can be useful for creating subsets of data based on specific criteria, such as mapping only sales data for a particular region or time period.
Java Code Listing
package com.northconcepts.datapipeline.foundations.examples.datamapping; import com.northconcepts.datapipeline.core.Record; import com.northconcepts.datapipeline.foundations.datamapping.DataMapping; import com.northconcepts.datapipeline.foundations.datamapping.DataMappingResult; import com.northconcepts.datapipeline.internal.expression.DefaultExpressionContext; public class ConditionallyMapDataFromSourceToTarget { public static void main(String[] args) { DefaultExpressionContext input = new DefaultExpressionContext(); input.setValue("fname", "John"); input.setValue("lname", "Smith"); DataMapping mapping = new DataMapping() .addCondition("length(source.fname) > 3") // don't map anything if fname is not greater than 3 characters .addFieldMapping("first_name", "source.fname") .addFieldMapping("length", "length(source.fname)"); DataMappingResult result = mapping.map(input); Record target = result.getTarget(); System.out.println("target = " + target); result.logExceptions(); } }
Code Walkthrough
- DefaultExpressionContext instance is used to hold the source/input data with the key-value pattern. Values are entered with
setValue()
method. - DataMapping is created to enable the field mapping rules.
- Checking for a condition is applied with
addCondition()
method. The expression in the example checks whether the length ofsource.fname
field is greater than 3. Only if this condition is met, the next steps are implemented. addFieldMapping()
method accepts two String arguments:targetFieldName
- the name of the field in the target record.sourceExpression
- a custom expression to hold the formatting logic for a target field.
- Fields in the source and target records can be accessed with the dot (.) operator:
source.fname
. - Different methods for formatting strings can be used in sourceExpression. In the given example, length() is called.
- DataMappingResult instance is created to apply mapping for the input.
- The target record is obtained from
result
object and printed in the console withSystem.out.println()
. - The exceptions from the result, if exist, are also logged.
Console output
target = Record (MODIFIED) { 0:[first_name]:STRING=[John]:String 1:[length]:LONG=[4]:Long }