Created 02-13-2025 01:59 AM
Is there a way to overwrite an existing file with PutFile processor?Nifi version is 1.23.2
Created 02-14-2025 08:43 AM
@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.
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.
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
Created 02-13-2025 06:18 AM
@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
Created 02-13-2025 07:11 AM
I do not want to change the content of the file, I want to print a new value over the existing value.
Created 02-13-2025 07:52 AM
@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
Created 02-13-2025 11:44 PM
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
Created 02-14-2025 08:43 AM
@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.
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.
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
Created 02-14-2025 01:22 PM
Thank you very much for your answer 🙂
I tried it and found it worked.