Wednesday 31 May 2017

Dell Boomi: Update Flat File/CSV data using groovy script

Update data of CSV file using Groovy

In this blog i am trying to share my experience of updating csv data using groovy scripts. I had an explicit requirement that i have to remove leading zeros from a integrer data field before passing it to my map.

As you all work on Dell Boomi so you know that in Boomi there are so many built in Strings function which can do this task very easily or you can use a simple javascript code to achieve this functionality.

Javascript code:

Output = Input.replace(/^0+/, '') ;


I have to do this by groovy because in the data process you can only use groovy script and i have to remove leading zeros before passing it to map function.

Sample file:

test-trstevens113@msn.com,Test User,0000100098,8797784047620,trstevenstest@msn.com,,,0,2016-01-18 11:36:32.847,0000123652
test-tstevens@lves.washk12.org,Test2 User,0000100098,87976289823652,tstevens222@lves.washk12.org,,,0,2016-01-18 11:35:45.327,000012352


Note:
I have to remove leading zeros from column 3 that is "0000100098" but in code below i use 2 as this is indexed with zero.

Code of Groovy:

import java.util.Properties;
import java.io.InputStream;
import java.io.BufferedReader;


String DELIMITER = ",";

for( int i = 0; i < dataContext.getDataCount(); i++ ) {

   InputStream is = dataContext.getStream(i);
   Properties props = dataContext.getProperties(i);

   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
   StringBuffer outData = new StringBuffer();
   int lineNum = 0;
      
   while ((line = reader.readLine()) != null) {
// Parse the line into separate columns splitting on the delimiter defined above
String[] columns = line.split(DELIMITER);

         // Obtain a column's value by its known position on the line (zero-indexed)
String someColumnValue = columns[2];

columns[2] = someColumnValue.replaceFirst("^0+(?!\$)", "");
for(int k = 0; k < columns.length; k++){
outData = outData.append(columns[k]+DELIMITER);

//outData.append('\n');
}
    
      lineNum++;
   }
   
   // Convert the output StringBuffer to an InputStream and store in the dataContext
   is = new ByteArrayInputStream(outData.toString().getBytes("UTF-8"));
   dataContext.storeStream(is, props);
   
}

Hope this helps you. Happy Coding !!!

1 comment:

  1. i would like to thank you for ur knowledgeble post . for more information visit our website DellBhomi Online Training

    ReplyDelete