Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Is possible to write an attribute into a file and also keep the original flow file?

avatar

As the title says, I want to write down into a file a nifi attribute, but maintaining the original flowfile. I have a external .jar that only accepts files, so I need to write an attribute into a file, and syncronhize the output with the corresponding flowfile.

Thanks in advance.

5 REPLIES 5

avatar
Master Mentor

@Alejandro A.

You can use the replaceText processor to do this. It offers prepend and append replacement strategies.

Just provide the name of the FlowFile Attribute you which to append or prepend in the "Replacement Value:" property of the processor in the form of a NiFi Expression Language statement.

${<flowfile-attribute-name>}

Thanks,

Matt

avatar
Expert Contributor
@Alejandro A.

Are you saying you would like, at the end of this particular portion of your flow, to have the original content in a flowfile, and a second flowfile with the output generated by your external jar?

If so, there are a few ways you could do this... One of them would be to use a DuplicateFlowFile processor to create a second copy of your flowfile, and use a ReplaceText processor on flowfile with the attribute value as content. You can use the Wait and Notify processors to wait for the processing of that flowfile. An example usage of the Wait/Notify processors can be found here.

For the Release Signal Identifier, you can use ${filename} as the example suggests, but if your filenames aren't unique, you could use an UpdateAttribute processor to capture the original UUID of the flowfile before the DuplicateFlowFile processor.

This is probably the easiest way to be able to know when that second flowfile has been processed. You could use MergeContent with a Correlation Attribute Name set to the same value as the Release Signal Identifier (and Max Number of Entries set to 2), and make sure the original flowfile gets routed from its Wait processor success relationship to the MergeContent processor, along with the success relationship of the second flowfile.

If you're processing many different files concurrently, make sure that Maximum Number of Bins is equal to or greater than the number of concurrent files.

I could probably create a sample flow of this, if you have trouble putting it together.

avatar
Expert Contributor

@Alejandro A.

Did this answer end up solving your use case?

avatar

Finally I made my own script. Something like that

//WRITE AN ATTRIBUTE INTO A FILE
var flowFile = session.get();
var File = Java.type("java.io.RandomAccessFile");
if (flowFile != null) {
    var filename = flowFile.getAttribute("filename");
    var lang = flowFile.getAttribute("lang");
    var text = flowFile.getAttribute("text");
    /// write to file
    var filePath = "/home/test/" + lang + "/inputData/" + filename;
    flowFile = session.putAttribute(flowFile, "filePath", filePath);
    var file = new File(filePath, "rw");
        file.write(text.getBytes());
        file.close();
    // Finish by transferring the FlowFile to an output relationship
    session.transfer(flowFile, REL_SUCCESS);
}

avatar

Thanks so much