Support Questions

Find answers, ask questions, and share your expertise

How to Change Content of flowfile provided property name dynamically.

avatar
Contributor

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 

mohdriyaz_0-1713685742848.png     

here is the flowfile attribute 

mohdriyaz_1-1713685829562.png

I want to change the value of "id"  in flowfile content using UpdateRecords like this : 

( WHICH IS NOT WORKING )

mohdriyaz_2-1713685901848.png

 

This works but add a new property to existing flow file 

mohdriyaz_0-1713686303606.png

mohdriyaz_1-1713686349403.png

Here "id" got change because i provided "/id" (hardcoded property name) , i need same functionality to work with "/${col_name}"

 

Any Pointers ... ?????

Thanks,

Riyaz

1 ACCEPTED SOLUTION

avatar
Super Guru

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

 

View solution in original post

2 REPLIES 2

avatar
Super Guru

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

 

avatar
Contributor

Used Option2 - Groovy script for this scenario.