Specify Lookup Values Programmatically

Updated: Jun 27, 2023

In this example, you will learn how you can use Data Pipeline to perform data lookups in a file and combine the retrieved information with the specified values, based on a common field. It provides a flexible and dynamic way to augment data by combining external data sources with user-defined values, enhancing the dataset's completeness and accuracy.

The example can be used to enable ad hoc analysis or data processing tasks where the dataset needs to be augmented with specific values or attributes based on the user's requirements.

Input CSV files

  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
  1.  

Java Code Listing

  1. package com.northconcepts.datapipeline.examples.cookbook;
  2.  
  3. import java.io.File;
  4.  
  5. import com.northconcepts.datapipeline.core.DataReader;
  6. import com.northconcepts.datapipeline.core.FieldList;
  7. import com.northconcepts.datapipeline.core.StreamWriter;
  8. import com.northconcepts.datapipeline.csv.CSVReader;
  9. import com.northconcepts.datapipeline.job.Job;
  10. import com.northconcepts.datapipeline.transform.TransformingReader;
  11. import com.northconcepts.datapipeline.transform.lookup.BasicLookup;
  12. import com.northconcepts.datapipeline.transform.lookup.LookupTransformer;
  13.  
  14. public class SpecifyLookupValuesProgrammatically {
  15. public static void main(String[] args) throws Throwable {
  16. DataReader reader = new CSVReader(new File("example/data/input/credit-balance-01.csv"))
  17. .setFieldNamesInFirstRow(true);
  18. // create a lookup that returns a field named 'rating_description'
  19. BasicLookup lookup = new BasicLookup(new FieldList("rating_description"))
  20. .add("A", "Class-A")
  21. .add("B", "Class-B")
  22. .add("C", "Class-C");
  23. // use 'Rating' from example/data/input/credit-balance-01.csv
  24. reader = new TransformingReader(reader)
  25. .add(new LookupTransformer(new FieldList("Rating"), lookup));
  26. Job.run(reader, new StreamWriter(System.out));
  27. }
  28. }
  29.  

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. BasicLookup is created to specify additional information about Ratings. A new field rating_description is added with three rows.
  4. TransformingReader is a proxy that applies transformations to records passing through.
  5. The lookup matches "credit-balance-01.csv".[Rating] with the first column of the BasicLookup instance. The reader contains the full data from credit-balance-01.csv along with the rating_description field from the BasicLookup. 
  6. Data is transferred from the reader to the CSVWriter via Job.run() method. 

Output

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