It appears the best way to do this is to apply the java getSize() method to
the ff object, like this:
def ffSize = ff.getSize()
This seems to output the file size to the log in bytes.
def file_size = (ffSize != null) ? ffSize :0
if (file_size == 0) {
log.error("file_size is undefined or zero, which prevents division.")
session.transfer(ff, REL_FAILURE)
return
}
log.info("File size: ${file_size}")
On Sat, Jun 15, 2024 at 10:43 PM James McMahon <[email protected]> wrote:
> It turns out you can access it in a Groovy script directly through ff, as
> ff.size. It appears to return a result in bytes.
>
> import org.apache.nifi.processor.ProcessContext
> import org.apache.nifi.processor.ProcessSession
> import org.apache.nifi.processor.io.InputStreamCallback
> import org.apache.nifi.flowfile.FlowFile
>
> def ff = session.get()
> if (!ff) return // Exit if no flow file is available
>
> try {
> // Retrieve and log all attributes
> ff.getAttributes().each { key, value ->
> log.info("Attribute: ${key} = ${value}")
> }
>
> // Log the fileSize system attribute
> log.info("System Attribute - fileSize: ${ff.size}")
>
> ff = session.putAttribute(ff, 'file_size', ff.size.toString())
>
> // Transfer the flow file to the next stage in the flow
> session.transfer(ff, REL_SUCCESS)
> } catch (Exception e) {
> log.error("Error processing flow file", e)
> session.transfer(ff, REL_FAILURE)
> }
>
> On Sat, Jun 15, 2024 at 5:14 PM James McMahon <[email protected]>
> wrote:
>
>> Hello. I am trying to determine a way to get the size of a NiFi flowfile
>> within a Groovy script. Has anyone done this before, and can tell me how to
>> do this from within Groovy?
>>
>> This
>> https://jameswing.net/nifi/nifi-internal-fields.html
>> describes fileSize as a hidden field that can be accessed via expression
>> language, but I do not see how I can reference that in a Groovy script.
>>
>> Thanks in advance for any help.
>>
>