Support Questions

Find answers, ask questions, and share your expertise

NiFi: Capture filename and filesize

avatar
Expert Contributor

How to capture the filesize and the filename, which can be found in the details section of the flowfile? I understand that the filename is listed in the attribute section, but is there a way to know the variable name for filesize?

kafka-flow-file.jpg

1 ACCEPTED SOLUTION

avatar
Master Mentor

@kumar

The default FlowFile attributes include:

entryDate

lineageStartDate

fileSize

filename

path

uuid

The above FlowFile attribute key names are case sensitive.

Thanks,

Matt

View solution in original post

3 REPLIES 3

avatar
Master Guru

I don't know of any out of box functionality to get file size. You can use the execute script to and script up way to easily grab the file size. Or build a processor to grab content size. Here is what the code for the processor would look like. very simple.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog log = getLogger();
    final AtomicReference<String> value = new AtomicReference<>();

    FlowFile flowfile = session.get();

    session.read(flowfile, new InputStreamCallback() {
        @Override
        public void process(InputStream in) throws IOException {
            try{

                String outputHandler = context.getProperty(OUTPUTHANDLER).getValue();


                String stringToTest = IOUtils.toString(in);

                // Check length, in characters if that is what you want
                System.out.println(stringToTest.length()); // prints "11"

                // Or provided specific encoded sizes ie UTF-8. this can be property driven
                //String result = stringToTest.getBytes("UTF-8").toString();

                value.set(result);
            }catch(Exception ex){
                ex.printStackTrace();

avatar
Master Mentor

@kumar

The default FlowFile attributes include:

entryDate

lineageStartDate

fileSize

filename

path

uuid

The above FlowFile attribute key names are case sensitive.

Thanks,

Matt

avatar
Expert Contributor

Thank you, that was it! It would good if they get listed in the Attribute tab as well.