Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

NiFi - Script count fileFlows Groovy

NiFi - Script count fileFlows Groovy

New Contributor

Hi, I need create groovy script to count th fileflows, this is my script:

def trigger = session.get() 
def flowFiles = session.get() 
if(flowFiles.size() < 9) { 
session.transfer(flowFiles,REL_SUCCESS) 
}else { 
session.transfer(flowFiles, REL_FAILURE) }

I want launch a script if de fileflow files are < 9.

But the error is:

92787-sin-titulo.jpg

5 REPLIES 5

Re: NiFi - Script count fileFlows Groovy

session.get() only returns a FlowFile reference (or null if one is not available). Instead you want something like

session.get(9)

which will return a List<FlowFile> guaranteed not to be null, but it may have size < 9 so your check should work in this case. Having said that, are you sure you want to transfer them to failure? If you're planning on just routing them back to the ExecuteScript (until 9 are available) then you could just do session.rollback() or session.transfer(flowFile, Relationship.SELF)

Re: NiFi - Script count fileFlows Groovy

I'm not sure if I fully understand how your downstream flow would work when there are < 9 flow files available to ExecuteScript, but here's something to try (note that you can use anything greater than 9 in place of the "10" below):

def flowFiles = session.get(10)
if(flowFiles.size() >= 9) {
session.remove(flowFiles)
} else {
session.transfer(flowFiles, REL_SUCCESS)
}
Highlighted

Re: NiFi - Script count fileFlows Groovy

New Contributor

I just want to create a groovy script that counts the fileflows and if there is less than 9 run a script shell.

Thanks

Re: NiFi - Script count fileFlows Groovy

What about if there are zero available? Are you scheduling this processor to run every few seconds, or the default (as fast as possible)? If the latter, you will likely want to check that there are > 0 and < 9 flow files available.

Re: NiFi - Script count fileFlows Groovy

New Contributor

@Matt BurgessI had the similar scenario where i needed the count of flowfiles generated but the script is not working.The incoming queue is queued with the flowfiles and its not getting processed.Is it because its a clustered nifi.Can you please help?