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: Capture filename and filesize

Solved Go to solution

NiFi: Capture filename and filesize

Rising Star

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

Accepted Solutions

Re: NiFi: Capture filename and filesize

Master Guru

@kumar

The default FlowFile attributes include:

entryDate

lineageStartDate

fileSize

filename

path

uuid

The above FlowFile attribute key names are case sensitive.

Thanks,

Matt

3 REPLIES 3

Re: NiFi: Capture filename and filesize

Super 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();

Re: NiFi: Capture filename and filesize

Master Guru

@kumar

The default FlowFile attributes include:

entryDate

lineageStartDate

fileSize

filename

path

uuid

The above FlowFile attribute key names are case sensitive.

Thanks,

Matt

Re: NiFi: Capture filename and filesize

Rising Star

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

Don't have an account?
Coming from Hortonworks? Activate your account here