Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

How to generate UUID in apache NIFI ?

avatar
Contributor

I am developing a JSON to CSV converter job in NIFI and I have to generate a UUID for each json element and add that generated UUID to the flowfile. I don't find any suitable processor for generating UUID ? My question is how can I generate a UUID for each incoming flowfile.

4 REPLIES 4

avatar
Master Guru
@Vivek Singh

UUID is associated with each flowfile so you can use expression language like

${UUID()} 

to get the unique id associated with the flowfile.

avatar
Contributor

I want to generate manually. Should I go for execute script ?

avatar
Master Guru

@Vivek Singh

it's already randomly generated id in NiFi,

UUID Description:

Returns a randomly generated UUID.

Subject Type: No

Subject Arguments: No arguments

Return Type: String

Examples: ${UUID()} returns a value similar to de305d54-75b4-431b-adb2-eb6b9e546013

for more reference in this link

We cannot modify the "uuid" attribute associated with the flowfile using execute script also because it is fixed for a FlowFile, if the key is named "uuid", it will be ignored.
more details regarding execute scripts are in this link

How ever we can assaign new uuid that has been generated manually to new attribute and the new attribute name is something not "uuid"

just for reference sample groovy script to generate UUID

def flowFile = session.get()
if(!flowFile) return
def ruuid = UUID.randomUUID().toString()
flowFile = session.putAttribute(flowFile, 'uuid2', ruuid)
session.transfer(flowFile, REL_SUCCESS)
in this script we are just adding new attribute called as uuid2 to the flowfile and assigning randomuuid value to it.

Output:-

67406-uuid.png

For each flowfile we are going to have uuid which is assigned by NiFi and uuid2 assigned by the script.

Let us know if you are having any issues..!!

avatar
Master Guru
@Vivek Singh

Does the answer helped, then please Click on Accept button below to accept the answer, That would be great help to Community users to find solution quickly for these kind of issues.