Support Questions

Find answers, ask questions, and share your expertise

How to read only the header from a flowfile

avatar
Rising Star

I have a requirement where I will have to decide the flow based on the record header.

Since the file is huge, I don't want to waste time reading the entire file where only one record is enough.

Is there a way I can achieve this.

1 ACCEPTED SOLUTION

avatar
Master Guru
You can use ExecuteScript with Groovy and the following script (assuming your input is newline-delimited):
def flowFile = session.get()
if(!flowFile) return	
def header = ''
session.read(flowFile, { inStream -> 
   header = new BufferedReader(new InputStreamReader(inStream)).readLine()
} as InputStreamCallback)
flowFile = session.putAttribute(flowFile, 'header', header)
session.transfer(flowFile, REL_SUCCESS)

This puts the first line in an attribute called 'header', which you can use with RouteOnAttribute to decide where to send the flow. Note that this script doesn't do error handling, but you could put a try/catch around the session.read to session.transfer, the catch could route the flow file to REL_FAILURE.

View solution in original post

1 REPLY 1

avatar
Master Guru
You can use ExecuteScript with Groovy and the following script (assuming your input is newline-delimited):
def flowFile = session.get()
if(!flowFile) return	
def header = ''
session.read(flowFile, { inStream -> 
   header = new BufferedReader(new InputStreamReader(inStream)).readLine()
} as InputStreamCallback)
flowFile = session.putAttribute(flowFile, 'header', header)
session.transfer(flowFile, REL_SUCCESS)

This puts the first line in an attribute called 'header', which you can use with RouteOnAttribute to decide where to send the flow. Note that this script doesn't do error handling, but you could put a try/catch around the session.read to session.transfer, the catch could route the flow file to REL_FAILURE.