Support Questions

Find answers, ask questions, and share your expertise

putFile Overwrite File

avatar
Explorer

Is there a way to overwrite an existing file with PutFile processor?Nifi version is 1.23.2

1 ACCEPTED SOLUTION

avatar
Master Mentor

@hus 

Thank you for the clarification on your use case.

The only purpose built processor NiFi has for appending lines to an exsisting file is the putSyslog processor. But it is designed to work with syslog formatted messages being sent to a syslog server for RFC5424 and RFC3164 formatted messages and can't be used to append directly to a local file. 

However, your use case could be solved using the ExecuteStreamCommand processor and custom script.  The ExecuteStreamCommand processor passed a FlowFile's content to the input of the script.

Example:
I created the following script which I placed on my NiFi node somewhere where my NiFi service user has access.  Gave my NiFi service user execute permissions on the bash script (I named it file-append.sh)

#!/bin/bash

STD_IN=$(</dev/stdin)
touch $1/$2
echo "$STD_IN" >> $1/$2

This script will take stdin from the executeStreamCommand processor which will contain the content of the FlowFile being processed.  $1 and $2 are command arguments i define in the ExecuteStreamCommand processor which I use to dynamically define the path and filename the content will be appended to .  It then takes FlowFiles content and either start a new file or append to a file if it already exists with the passed filename.

MattWho_0-1739551036267.png

You can see that I set my two command arguments by pulling values from the "path" and "filename" NiFi FlowFile attributes set on the FlowFile being processed.

MattWho_1-1739551205997.png

With this dataflow design you can append lines to various files as needed.

 

Please help our community thrive. If you found any of the suggestions/solutions provided helped you with solving your issue or answering your question, please take a moment to login and click "Accept as Solution" on one or more of them that helped.

Thank you,
Matt

View solution in original post

6 REPLIES 6

avatar
Master Mentor

@hus 

The PutFile processor has a conflict resolution option of "replace".  When used and a file of the same filename already exists in target directory, it will be replaced with the file being written by putFile processor.

Please help our community thrive. If you found any of the suggestions/solutions provided helped you with solving your issue or answering your question, please take a moment to login and click "Accept as Solution" on one or more of them that helped.

Thank you,
Matt

avatar
Explorer

I do not want to change the content of the file, I want to print a new value over the existing value.

avatar
Master Mentor

@hus 

So what i am understanding is you do not want to overwrite the existing file, but rather update an existing file.  Is my understanding correct?

Can you share more detail or example of what you are trying to accomplish?
Are you looking for a way to search the content of an existing file for a specific string and the replace that string with a new string?

NiFi processors are designed to perform work against FlowFiles contained within NiFi, but there are processors that can be triggered by a FlowFile that can run a script against files outside of NiFi.

You could also ingest the file, modify its content and the write out the newly modified file.

Please help our community thrive. If you found any of the suggestions/solutions provided helped you with solving your issue or answering your question, please take a moment to login and click "Accept as Solution" on one or more of them that helped.

Thank you,
Matt

avatar
Explorer

Yes that's true.

I want to add the flowFiles content from the stream to the file line by line.For example two flowFiles came from the stream.The content of the first flowFile is content1.The content of the second flowFile is content2.As a result, when the flow is completed, I want the following two lines to be in the file on the server:

content1

content2

avatar
Master Mentor

@hus 

Thank you for the clarification on your use case.

The only purpose built processor NiFi has for appending lines to an exsisting file is the putSyslog processor. But it is designed to work with syslog formatted messages being sent to a syslog server for RFC5424 and RFC3164 formatted messages and can't be used to append directly to a local file. 

However, your use case could be solved using the ExecuteStreamCommand processor and custom script.  The ExecuteStreamCommand processor passed a FlowFile's content to the input of the script.

Example:
I created the following script which I placed on my NiFi node somewhere where my NiFi service user has access.  Gave my NiFi service user execute permissions on the bash script (I named it file-append.sh)

#!/bin/bash

STD_IN=$(</dev/stdin)
touch $1/$2
echo "$STD_IN" >> $1/$2

This script will take stdin from the executeStreamCommand processor which will contain the content of the FlowFile being processed.  $1 and $2 are command arguments i define in the ExecuteStreamCommand processor which I use to dynamically define the path and filename the content will be appended to .  It then takes FlowFiles content and either start a new file or append to a file if it already exists with the passed filename.

MattWho_0-1739551036267.png

You can see that I set my two command arguments by pulling values from the "path" and "filename" NiFi FlowFile attributes set on the FlowFile being processed.

MattWho_1-1739551205997.png

With this dataflow design you can append lines to various files as needed.

 

Please help our community thrive. If you found any of the suggestions/solutions provided helped you with solving your issue or answering your question, please take a moment to login and click "Accept as Solution" on one or more of them that helped.

Thank you,
Matt

avatar
Explorer

Thank you very much for your answer 🙂

I tried it and found it worked.