santhosh
santhosh

Reputation: 509

Nifi: append an flowfile attribute to the flowfile content (at the end)

I am using putdatabaserecord to store a value into DB.

I also have a flow based on failure link to (this processor), so that failed record will be stored in disc for troubleshooting.

I can store the failed record in disc by using putfile processor.

Now i wanted it to be more informative during debugging. As the attribute "putdatabaserecord.error" contains the error reason, i want to append the flowfile content with this error reason as well.

 I tried to achieve this using below flow. (attached visualization)

putsql/putdbrecord -> failure-> updateattribute (this is to give the flowfile a realistic name)-> attributesToJson (destination flowfileattribute/content -> putfile

but in attributesTOJson , if the destination is kept as flowfileattribute, the flowfile don't get this attribute and if destination is kept as flowfile content, original flowfile content is overrided.

My requirement is to append this single attribute to the existing flowfile content and not to override it.

Any help or suggestion is greatly appreciated.enter image description here

Upvotes: 0

Views: 789

Answers (1)

tonykoval
tonykoval

Reputation: 1257

Use a ScriptedTransformRecord processor:

  • Record Reader: JsonTreeReader
  • Record Writer: JsonRecordSetWriter
  • Script Language: Groovy
  • Script Body:
record.setValue("error_msg", attributes["putdatabaserecord.error"])
record

Input (JSON):

[
  { 
    "id": 280,
    "index_id": 1,
    "state_id":1
  }
] 

Output (JSON):

[
  { 
    "id": 280,
    "index_id": 1,
    "state_id": 1,
    "error_msg": "value from attribute"
  }
] 

Upvotes: 1

Related Questions