Support Questions
Find answers, ask questions, and share your expertise

NiFi - Script count fileFlows Groovy

Explorer

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

Super Guru

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)

Super Guru

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)
}

Explorer

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

Thanks

Super Guru

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.

@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?