Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

ExecuteScript - flowFile with new attributes not sent to next processor

avatar

I have a GenerateFlowFile processor that sends a flowFile to the ExecuteScript processor. In the ExecuteScript processor, I added a new attribute and value to the incoming flowFIle. But this updated flowFIle is not output to the next processor

Incoming FlowFile content from GenerateFlowFile

 

 

{'name':'myName'}

 

 


ExecuteScript script body

 

 

var flowFile = session.get();
if (flowFile != null) {
	try {
		flowFile = session.putAttribute(flowFile, 'gender', 'male');
		session.transfer(flowFile, REL_SUCCESS);
	} catch (e) {
		session.transfer(flowFile, REL_FAILURE);
	}
}

 

 

 

Output to next processor

 

 

{'name':'myName'}

 

 

Am I missing something??

1 ACCEPTED SOLUTION

avatar

Hi,

 

Thank you for pointing that out. It has certaintly speed up my development.

 

This is a short snippet to my solution:

 

var StreamCallback =  Java.type("org.apache.nifi.processor.io.StreamCallback");
var IOUtils = Java.type("org.apache.commons.io.IOUtils");
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");

var flowFile = session.get();
if(flowFile != null) {
	try {
	  // Create a new StreamCallback, passing in a function to define the interface method
	  flowFile = session.write(flowFile,
	    new StreamCallback(function(inputStream, outputStream) {
	        var text = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
					var obj = JSON.parse(text);
					obj["gender"] = "Male";
					outputStream.write(JSON.stringify(obj).getBytes(StandardCharsets.UTF_8));
	    }));
		session.transfer(flowFile, REL_SUCCESS);
	} catch (e) {
			log.error(e.message)
			session.transfer(flowFile, REL_FAILURE);
	}
}

 

Something to point out for those using javascript. If your using GenerateFlowFile processor to generate sample JSON content, the JSON content in Custom Text must use double quotes ( " ), not single quotes ( ' ). I spent an hour debugging why my JSON.parse(text) didn't work.

 

Custom Text

 

{'name':'myName'}

 

It should be

 

{"name":"myName"}

 

 

 

 

 

View solution in original post

2 REPLIES 2

avatar
Expert Contributor

Hello,

 

in your script body you dont manipulate the content.

You add an Attribute to your flowfile (which works 😀) 

 

Maybe you can have a look at https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018/page/...

 

But in case you don´t need to do it via ExecuteScript Processor, you could use JoltTransformJSON Processor.

 

On conficuration you can set for the property Jolt Specification following value:

[
  {
    "operation": "default",
    "spec": {
      "gender": "male"
    }
  }
]

 After that your JSON should be extended with your key-value-pair.

 

Greetings

avatar

Hi,

 

Thank you for pointing that out. It has certaintly speed up my development.

 

This is a short snippet to my solution:

 

var StreamCallback =  Java.type("org.apache.nifi.processor.io.StreamCallback");
var IOUtils = Java.type("org.apache.commons.io.IOUtils");
var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");

var flowFile = session.get();
if(flowFile != null) {
	try {
	  // Create a new StreamCallback, passing in a function to define the interface method
	  flowFile = session.write(flowFile,
	    new StreamCallback(function(inputStream, outputStream) {
	        var text = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
					var obj = JSON.parse(text);
					obj["gender"] = "Male";
					outputStream.write(JSON.stringify(obj).getBytes(StandardCharsets.UTF_8));
	    }));
		session.transfer(flowFile, REL_SUCCESS);
	} catch (e) {
			log.error(e.message)
			session.transfer(flowFile, REL_FAILURE);
	}
}

 

Something to point out for those using javascript. If your using GenerateFlowFile processor to generate sample JSON content, the JSON content in Custom Text must use double quotes ( " ), not single quotes ( ' ). I spent an hour debugging why my JSON.parse(text) didn't work.

 

Custom Text

 

{'name':'myName'}

 

It should be

 

{"name":"myName"}