Save and Load DataMapping to XML

This example shows how to save and load data mapping information to an XML file, providing you with a structured way to store and retrieve data mapping configurations. It enables you to persist their mapping settings, making it easy to reuse or share mappings across different data processing tasks.

 

Java Code Listing

package com.northconcepts.datapipeline.foundations.examples.datamapping;

import java.io.FileInputStream;
import java.io.FileWriter;

import com.northconcepts.datapipeline.core.FieldType;
import com.northconcepts.datapipeline.foundations.datamapping.DataMapping;
import com.northconcepts.datapipeline.foundations.datamapping.FieldMapping;
import com.northconcepts.datapipeline.foundations.schema.BooleanFieldDef;
import com.northconcepts.datapipeline.foundations.schema.EntityDef;
import com.northconcepts.datapipeline.foundations.schema.TextFieldDef;

public class SaveAndLoadDataMappingToXML {

    public static void main(String[] args) throws Throwable {

        // Save DataMapping to XML file.
        DataMapping mapping = new DataMapping()
                .setName("Customer Details")
                .addFieldMapping(new FieldMapping("owner", "${source.customerid}").setCondition("${source.customerid} == 10001"))
                .addFieldMapping(new FieldMapping("first_name", "${source.customer_fname}"))
                .addFieldMapping(new FieldMapping("last_name", "${source.customer_lname}"))
                .addFieldMapping(new FieldMapping("name", "${target.first_name} + ' ' + ${target.last_name}"))
                .addFieldMapping(new FieldMapping("phone", "${source.customer-contact}"))
                .addFieldMapping(new FieldMapping("email", "${source.customer-email}"))
                .addFieldMapping(new FieldMapping("isActive", "${source.customer-active}"));

        EntityDef entityDef = new EntityDef()
                .setName("Order Details")
                .addField(new TextFieldDef("OrderName", FieldType.STRING)
                            .setRequired(true)
                            .setMinimumLength(0)
                            .setMaximumLength(2))
                .addField(new BooleanFieldDef("isActiveOrder", FieldType.BOOLEAN));

        mapping.setTargetValidationEntity(entityDef);

        mapping.toXml(new FileWriter("example/data/output/customer-details-data-mapping.xml"), true);
        
        //Load DataMapping from this XML File.
        DataMapping mappingFromXml = new DataMapping()
                .fromXml(new FileInputStream("example/data/output/customer-details-data-mapping.xml"));
        
        System.out.println("DataMapping loaded from XML:-\n" + mappingFromXml);
    }

}

 

Code Walkthrough

  1. DataMapping called "Customer Details" is created to enable the field mapping rules. addFieldMapping() method accepts FieldMapping object that has two String arguments:
    1. targetFieldName - the name of the field in the target record.
    2. sourceExpression - a custom expression to hold the formatting logic for a target field.
  2. Fields in the source and target records can be accessed with the dot (.) operator: source.customeridtarget.last_name.
  3. EntityDef instance called "Order Details" is created with the following fields: OrderName, isActiveOrder.
  4. The entity created in the previous step is set as a target validation entity.
  5. The mapping is saved/exported to a new output file customer-details-data-mapping.xml.
  6. Data mapping is then loaded from the file created in the previous step with the FileInputStream object.
  7. Data is printed in the console with System.out.println() method.

 

Console Output

DataMapping loaded from XML:-
{
  "autoMapping" : false,
  "fieldMappings" : [ {
    "conditionAsString" : "${source.customerid} == 10001",
    "sourceExpressionAsString" : "${source.customerid}",
    "targetFieldName" : "owner"
  }, {
    "sourceExpressionAsString" : "${source.customer_fname}",
    "targetFieldName" : "first_name"
  }, {
    "sourceExpressionAsString" : "${source.customer_lname}",
    "targetFieldName" : "last_name"
  }, {
    "sourceExpressionAsString" : "${target.first_name} + ' ' + ${target.last_name}",
    "targetFieldName" : "name"
  }, {
    "sourceExpressionAsString" : "${source.customer-contact}",
    "targetFieldName" : "phone"
  }, {
    "sourceExpressionAsString" : "${source.customer-email}",
    "targetFieldName" : "email"
  }, {
    "sourceExpressionAsString" : "${source.customer-active}",
    "targetFieldName" : "isActive"
  } ],
  "name" : "Customer Details",
  "rootExpressionContext" : {
    "parent" : { }
  },
  "rownum" : 0,
  "targetEntity" : {
    "addMissingOptionalFields" : false,
    "allowExtraFieldsInMapping" : true,
    "allowExtraFieldsInValidation" : true,
    "attributes" : { },
    "fields" : [ {
      "allowBlank" : true,
      "array" : false,
      "attributes" : { },
      "entityQualifiedName" : "Order Details.OrderName",
      "limitToAllowedValues" : false,
      "maximumLength" : 2,
      "minimumLength" : 0,
      "name" : "OrderName",
      "primaryKey" : false,
      "qualifiedName" : "Order Details.OrderName",
      "required" : true,
      "strictArrays" : true,
      "tags" : { },
      "type" : "STRING"
    }, {
      "array" : false,
      "attributes" : { },
      "entityQualifiedName" : "Order Details.isActiveOrder",
      "limitToAllowedValues" : false,
      "name" : "isActiveOrder",
      "primaryKey" : false,
      "qualifiedName" : "Order Details.isActiveOrder",
      "required" : false,
      "strictArrays" : true,
      "tags" : { },
      "type" : "BOOLEAN"
    } ],
    "name" : "Order Details",
    "qualifiedName" : "Order Details",
    "tags" : { }
  }
}
Mobile Analytics