How to read only the first line of data using Groovy

Document created by mike_c_frazier Employee on Apr 3, 2013
Version 1Show Document
  • View in full screen mode
You receive input data that can be in more than 1 file format. The file format is designated by the first row in the file. You need to read ONLY the first line of a flat file or CSV document (e.g. the header) to analyze the file format based on the header, before processing the data
Use a Data Process step with Custom Scripting. Replace the sample script with that below.

     Code:

// This script reads the first line out of each document and outputs only that line, ignoring the remaining lines.

newline = System.getProperty("line.separator");


for ( int i = 0; i < dataContext.getDataCount(); i++ ) {
       InputStream is = dataContext.getStream(i);
       Properties props = dataContext.getProperties(i);

       reader = new BufferedReader(new InputStreamReader(is));
       outData = newStringBuffer();

       while ( (line = reader.readLine()) != null ) {

          // Break after the first line
          outData.append(line);
          outData.append(newline);
          break;
      }

     is = new ByteArrayInputStream(outData.toString().getBytes("UTF-8"));

     dataContext.storeStream(is, props);
}

1 person found this helpful

Attachments

    Outcomes