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

7 REPLIES 7

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

Hi @mburgess 

Is there a way to get the number of the files in the input then assign the count value to an attribute and assign a number value to each file

 

I have files to merge using MergeContent so I should assign fragment.index for each file and fragment.count as total count of files to merge.

Community Manager

@yamaga, as this is an older post, you would have a better chance of receiving a resolution by starting a new thread. This will also be an opportunity to provide details specific to your environment that could aid others in assisting you with a more accurate answer to your question. You can link this thread as a reference in your new post.



Regards,

Vidya Sargur,
Community Manager


Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.
Learn more about the Cloudera Community:

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?

; ;