Rename Duplicate Fields

Updated: Dec 26, 2023

This example shows how to rename fields while allowing the result to contain multiple fields with the same name.  Normally, the RenamField transformer does not allow duplicate names.  However, the default behavior can be changed using the flag in this example.

 

Input CSV file

  1. Account,LastName,FirstName,Balance,CreditLimit,AccountCreated,Rating
  2. 101,Reeves,Keanu,9315.45,10000.00,1/17/1998,A
  3. 312,Butler,Gerard,90.00,1000.00,8/6/2003,B
  4. 868,Hewitt,Jennifer Love,0,17000.00,5/25/1985,B
  5. 761,Pinkett-Smith,Jada,49654.87,100000.00,12/5/2006,A
  6. 317,Murray,Bill,789.65,5000.00,2/5/2007,C

 

Java Code Listing

  1. package com.northconcepts.datapipeline.examples.cookbook;
  2.  
  3. import com.northconcepts.datapipeline.core.DataReader;
  4. import com.northconcepts.datapipeline.core.StreamWriter;
  5. import com.northconcepts.datapipeline.csv.CSVReader;
  6. import com.northconcepts.datapipeline.job.Job;
  7. import com.northconcepts.datapipeline.transform.RenameField;
  8. import com.northconcepts.datapipeline.transform.TransformingReader;
  9.  
  10. import java.io.File;
  11.  
  12. public class RenameDuplicateFields {
  13. public static void main(String[] args) {
  14. DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))
  15. .setFieldNamesInFirstRow(true);
  16.  
  17. reader = new TransformingReader(reader)
  18. .add(new RenameField("LastName", "Name"))
  19. .add(new RenameField("FirstName", "Name").setAllowDuplicateFieldNames(true))
  20. .add(new RenameField("Balance", "CreditLimit").setAllowDuplicateFieldNames(true));
  21.  
  22. Job.run(reader, new StreamWriter(System.out));
  23. }
  24. }
  25.  

 

Code walkthrough

  1. CSVReader is created corresponding to the input file credit-balance-01.csv.
  2. The CSVReader.setFieldNamesInFirstRow(true) method is invoked to specify that the names specified in the first row should be used as field names.
  3. A TransformingReader is created to apply one or more transformations to the incoming data sequentially.
  4. RenameField object accepts oldName and newName String arguments. By default, renaming does not allow duplicate field names in the dataset. To enable this feature, we need to call setAllowDuplicateFieldNames(true) method. In the given example, the "LastName" and "FirstName" fields are renamed to "Name".
  5.  Data is transferred from the reader to the StreamWriter(System.out) via Job.run() method.

 

Console Output

  1. -----------------------------------------------
  2. 0 - Record {
  3. 0:[Account]:STRING=[101]:String
  4. 1:[Name]:STRING=[Reeves]:String
  5. 2:[Name]:STRING=[Keanu]:String
  6. 3:[CreditLimit]:STRING=[9315.45]:String
  7. 4:[CreditLimit]:STRING=[10000.00]:String
  8. 5:[AccountCreated]:STRING=[1/17/1998]:String
  9. 6:[Rating]:STRING=[A]:String
  10. }
  11.  
  12. -----------------------------------------------
  13. 1 - Record {
  14. 0:[Account]:STRING=[312]:String
  15. 1:[Name]:STRING=[Butler]:String
  16. 2:[Name]:STRING=[Gerard]:String
  17. 3:[CreditLimit]:STRING=[90.00]:String
  18. 4:[CreditLimit]:STRING=[1000.00]:String
  19. 5:[AccountCreated]:STRING=[8/6/2003]:String
  20. 6:[Rating]:STRING=[B]:String
  21. }
  22.  
  23. -----------------------------------------------
  24. 2 - Record {
  25. 0:[Account]:STRING=[868]:String
  26. 1:[Name]:STRING=[Hewitt]:String
  27. 2:[Name]:STRING=[Jennifer Love]:String
  28. 3:[CreditLimit]:STRING=[0]:String
  29. 4:[CreditLimit]:STRING=[17000.00]:String
  30. 5:[AccountCreated]:STRING=[5/25/1985]:String
  31. 6:[Rating]:STRING=[B]:String
  32. }
  33.  
  34. -----------------------------------------------
  35. 3 - Record {
  36. 0:[Account]:STRING=[761]:String
  37. 1:[Name]:STRING=[Pinkett-Smith]:String
  38. 2:[Name]:STRING=[Jada]:String
  39. 3:[CreditLimit]:STRING=[49654.87]:String
  40. 4:[CreditLimit]:STRING=[100000.00]:String
  41. 5:[AccountCreated]:STRING=[12/5/2006]:String
  42. 6:[Rating]:STRING=[A]:String
  43. }
  44.  
  45. -----------------------------------------------
  46. 4 - Record {
  47. 0:[Account]:STRING=[317]:String
  48. 1:[Name]:STRING=[Murray]:String
  49. 2:[Name]:STRING=[Bill]:String
  50. 3:[CreditLimit]:STRING=[789.65]:String
  51. 4:[CreditLimit]:STRING=[5000.00]:String
  52. 5:[AccountCreated]:STRING=[2/5/2007]:String
  53. 6:[Rating]:STRING=[C]:String
  54. }
  55.  
  56. -----------------------------------------------
  57. 5 records
Mobile Analytics