Created on 04-21-2024 12:54 AM - edited 04-21-2024 01:22 AM
Hi,
I want to change the flowfile content (json format) using UpdateRecord by providing property (to be change) from attribute.
Here is the flowfile content
here is the flowfile attribute
I want to change the value of "id" in flowfile content using UpdateRecords like this :
( WHICH IS NOT WORKING )
This works but add a new property to existing flow file
Here "id" got change because i provided "/id" (hardcoded property name) , i need same functionality to work with "/${col_name}"
Any Pointers ... ?????
Thanks,
Riyaz
Created 04-21-2024 05:53 AM
Hi @mohdriyaz ,
I dont think you can accomplish this using the UpdateRecord processor as the added property has to be valid record path where the fields are specified explicitly. I can think of two options to do this:
1- Using JoltTransformJson: you can use "modify-overwrite-beta" spec to update a field but since the spec allows expression language and flowfile attribute evaluation you can reference the field name in the spec dynamically. In this case the spec will look like this:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"${col_name}":"9999"
}
}
]
2-Using ExecuteScript: You can write custom script like groovy, python ...etc. (depending on what Nifi version you are using) the script will basically read the json content into some dictionary or hashmap structure, read the flowfile attribute "col_name" into a variable and use that variable as a key to the field that you need to change in the json dictionary. Finally write then new content to the flowfile and pass to the success relationship. You can do error handling there as well and send any errors to the failure relationship. For more info on how to update flowfile content using ExecuteScript processor please refer to the "Reading and writing to/from a flow file" section under this link: https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018
If you find this is helpful please accept solution.
Thanks
Created 04-21-2024 05:53 AM
Hi @mohdriyaz ,
I dont think you can accomplish this using the UpdateRecord processor as the added property has to be valid record path where the fields are specified explicitly. I can think of two options to do this:
1- Using JoltTransformJson: you can use "modify-overwrite-beta" spec to update a field but since the spec allows expression language and flowfile attribute evaluation you can reference the field name in the spec dynamically. In this case the spec will look like this:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"${col_name}":"9999"
}
}
]
2-Using ExecuteScript: You can write custom script like groovy, python ...etc. (depending on what Nifi version you are using) the script will basically read the json content into some dictionary or hashmap structure, read the flowfile attribute "col_name" into a variable and use that variable as a key to the field that you need to change in the json dictionary. Finally write then new content to the flowfile and pass to the success relationship. You can do error handling there as well and send any errors to the failure relationship. For more info on how to update flowfile content using ExecuteScript processor please refer to the "Reading and writing to/from a flow file" section under this link: https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018
If you find this is helpful please accept solution.
Thanks
Created on 05-09-2024 08:39 PM - edited 05-09-2024 08:39 PM
Used Option2 - Groovy script for this scenario.