Rename Fields Quickly in Flat And Tabular Data

In this example, you are going to see a fast way to rename fields when working with flat, tabular data.

FaseRenameField is a very efficient way to rename fields and an alternative to RenameField that assumes:

  1. A flat table structure (no nested fields or arrays)
  2. Columns/Fields are always in the same position from row to row
  3. No missing fields (nulls field values are okay)
  4. The target name does not already exist (otherwise there will be two fields with the same name)

Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import java.io.File;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.csv.CSVReader;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.transform.FastRenameField;
import com.northconcepts.datapipeline.transform.TransformingReader;

public class RenameAFieldQuicklyInFlatAndTabularData {

    public static void main(String[] args) throws Throwable {
        DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))
                .setFieldNamesInFirstRow(true);

        reader = new TransformingReader(reader)
                .add(new FastRenameField("Rating", "Score")) // Rename field with name
                .add(new FastRenameField(1, "last_name")) // Rename field "LastName" to "last_name" by index
                .add(new FastRenameField(2, "first_name")) // Rename field "LastName" to "last_name" by index
                ;

        Job.run(reader, new StreamWriter(System.out));
    }

}

Output

00:39:57,664 DEBUG [main] datapipeline:37 - DataPipeline v8.1.0-SNAPSHOT by North Concepts Inc.
00:39:58,092 DEBUG [main] datapipeline:615 - Job[1,job-1,Sun Jan 08 00:39:58 IST 2023]::Start
-----------------------------------------------
0 - Record {
    0:[Account]:STRING=[101]:String
    1:[last_name]:STRING=[Reeves]:String
    2:[first_name]:STRING=[Keanu]:String
    3:[Balance]:STRING=[9315.45]:String
    4:[CreditLimit]:STRING=[10000.00]:String
    5:[AccountCreated]:STRING=[1/17/1998]:String
    6:[Score]:STRING=[A]:String
}

-----------------------------------------------
1 - Record {
    0:[Account]:STRING=[312]:String
    1:[last_name]:STRING=[Butler]:String
    2:[first_name]:STRING=[Gerard]:String
    3:[Balance]:STRING=[90.00]:String
    4:[CreditLimit]:STRING=[1000.00]:String
    5:[AccountCreated]:STRING=[8/6/2003]:String
    6:[Score]:STRING=[B]:String
}

-----------------------------------------------
2 - Record {
    0:[Account]:STRING=[868]:String
    1:[last_name]:STRING=[Hewitt]:String
    2:[first_name]:STRING=[Jennifer Love]:String
    3:[Balance]:STRING=[0]:String
    4:[CreditLimit]:STRING=[17000.00]:String
    5:[AccountCreated]:STRING=[5/25/1985]:String
    6:[Score]:STRING=[B]:String
}

-----------------------------------------------
3 - Record {
    0:[Account]:STRING=[761]:String
    1:[last_name]:STRING=[Pinkett-Smith]:String
    2:[first_name]:STRING=[Jada]:String
    3:[Balance]:STRING=[49654.87]:String
    4:[CreditLimit]:STRING=[100000.00]:String
    5:[AccountCreated]:STRING=[12/5/2006]:String
    6:[Score]:STRING=[A]:String
}

-----------------------------------------------
4 - Record {
    0:[Account]:STRING=[317]:String
    1:[last_name]:STRING=[Murray]:String
    2:[first_name]:STRING=[Bill]:String
    3:[Balance]:STRING=[789.65]:String
    4:[CreditLimit]:STRING=[5000.00]:String
    5:[AccountCreated]:STRING=[2/5/2007]:String
    6:[Score]:STRING=[C]:String
}

-----------------------------------------------
5 records
00:40:00,736 DEBUG [main] datapipeline:661 - job::Success
Mobile Analytics