Skip to main content
sathish3389
Silver ⭐️⭐️
August 30, 2022
Solved

Nifi

Hi,

Input is getting from jolt transforms and data is something like :

{
"dv_sys_id" : [ "23ec0223138d73006877", "983f853edb2c77007166f", "6a3d0a23138d73006877b3", "805d5339db8dd050f66" ]
}

output should go in invokehttp processor somthing like:

 

dv_sys_id: 23ec0223138d73006877, 983f853edb2c77007166f, 6a3d0a23138d73006877b3, 805d5339db8dd050f66

 

remote url : https://<url>${dv_sys_id}

 

How can i achieve this requirement, Pls help me out. 

 

 

 

araujo
araujoAnswer
Platinum ⭐️⭐️⭐️
August 30, 2022

@sathish3389 ,

 

One way to do this is to use the ExecuteScript processor with a script like the one below. This script with set the attribute dv_sys_id with the content that you want and will also add that to the content of the flowfile.

from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
import json

class PyStreamCallback(StreamCallback):
def __init__(self):
self.dv_sys_id = None

def process(self, inputStream, outputStream):
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
obj = json.loads(text)
self.dv_sys_id = 'dv_sys_id=%s' % (','.join(obj['dv_sys_id'],))
outputStream.write(bytearray(self.dv_sys_id.encode('utf-8','ignore')))

flow_file = session.get()
if flow_file != None:
callback = PyStreamCallback()
flow_file = session.write(flow_file, callback)
flow_file = session.putAttribute(flow_file, 'dv_sys_id', callback.dv_sys_id)
session.transfer(flow_file, REL_SUCCESS)

 

 

Cheers,

André

 

-- Was your question answered? Please take some time to click on "Accept as Solution" below this post. If you find a reply useful, say thanks by clicking on the thumbs up button.

4 replies

araujo
Platinum ⭐️⭐️⭐️
araujoAnswer
Platinum ⭐️⭐️⭐️
August 30, 2022

@sathish3389 ,

 

One way to do this is to use the ExecuteScript processor with a script like the one below. This script with set the attribute dv_sys_id with the content that you want and will also add that to the content of the flowfile.

from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from org.apache.nifi.processor.io import StreamCallback
import json

class PyStreamCallback(StreamCallback):
def __init__(self):
self.dv_sys_id = None

def process(self, inputStream, outputStream):
text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
obj = json.loads(text)
self.dv_sys_id = 'dv_sys_id=%s' % (','.join(obj['dv_sys_id'],))
outputStream.write(bytearray(self.dv_sys_id.encode('utf-8','ignore')))

flow_file = session.get()
if flow_file != None:
callback = PyStreamCallback()
flow_file = session.write(flow_file, callback)
flow_file = session.putAttribute(flow_file, 'dv_sys_id', callback.dv_sys_id)
session.transfer(flow_file, REL_SUCCESS)

 

 

Cheers,

André

 

Translate
-- Was your question answered? Please take some time to click on "Accept as Solution" below this post. If you find a reply useful, say thanks by clicking on the thumbs up button.
sathish3389
Silver ⭐️⭐️
August 31, 2022

Thanks for your response Andre

Translate
SS-dev
Silver ⭐️
February 7, 2024

Can someone help me out with a way to dynamically add attributes to a processor in nifi.
@sathish3389 @araujo 

Translate
DianaTorres
Community Manager
Community Manager
February 7, 2024

@SS-dev Welcome to the Cloudera Community!

As this is an older post, you would have a better chance of receiving a resolution by starting a new thread. This will also be an opportunity to provide details specific to your environment that could aid others in assisting you with a more accurate answer to your question. You can link this thread as a reference in your new post. Thanks.

Translate
Regards, Diana Torres, Senior Community Moderator