Write my own Transformer

Updated: Aug 24, 2023

This example offers a customizable Transformer component that enables you to modify the default data transformation logic. By utilizing this functionality, you can implement your own specialized data transformation rules tailored to specific data processing requirements and business logic.

This can be used in scenarios where the default data transformation provided by the library does not fully meet your needs. You may have unique data formats, complex data mappings, or specific data manipulation requirements that necessitate custom transformation rules. The ability to change the default Transformer allows you to achieve integration with your existing data infrastructure and ensure the data is processed in the desired format for further analysis.

 

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

WriteMyOwnTransformer.java

  1. package com.northconcepts.datapipeline.examples.cookbook.customization;
  2.  
  3. import java.io.File;
  4.  
  5. import com.northconcepts.datapipeline.core.DataReader;
  6. import com.northconcepts.datapipeline.core.StreamWriter;
  7. import com.northconcepts.datapipeline.csv.CSVReader;
  8. import com.northconcepts.datapipeline.job.Job;
  9. import com.northconcepts.datapipeline.transform.TransformingReader;
  10.  
  11. public class WriteMyOwnTransformer {
  12. public static void main(String[] args) throws Throwable {
  13. DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))
  14. .setFieldNamesInFirstRow(true);
  15. reader = new TransformingReader(reader)
  16. .setExceptionOnFailure(false)
  17. .add(new MyTransformer());
  18. Job.run(reader, new StreamWriter(System.out));
  19. }
  20. }
  21.  

 

MyTransformer.java

  1. package com.northconcepts.datapipeline.examples.cookbook.customization;
  2.  
  3. import com.northconcepts.datapipeline.core.Field;
  4. import com.northconcepts.datapipeline.core.Record;
  5. import com.northconcepts.datapipeline.transform.Transformer;
  6.  
  7. public class MyTransformer extends Transformer {
  8.  
  9. public MyTransformer() {
  10. }
  11.  
  12. public boolean transform(Record record) throws Throwable {
  13. Field creditLimit = record.getField("CreditLimit");
  14. // increase everyone's limit by 10%
  15. double newValue = Double.parseDouble(creditLimit.getValueAsString()) * 1.10;
  16. creditLimit.setValue(newValue);
  17. return true;
  18. }
  19.  
  20. }

 

Code Walkthrough

  1. CSVReader is created corresponding to the input file credit-balance-01.csv.
  2. The setFieldNamesInFirstRow(true) method is invoked to specify that the names specified in the first row should be used as field names.
  3. TransformingReader instance is created to sequentially apply changes to the incoming data. 
  4. MyTransformer which is a custom class to apply desired transformations is used to further process the data. Inside transform() method body, the CreditLimit value is increased by 10%, but this logic can be customized based on our needs.
  5. Job.run() is used to transfer the data from reader to StreamWriter(System.out). See how to compile and run data pipeline jobs. 

 

Console Output

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

 

Mobile Analytics