Created 11-08-2022 01:37 AM
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??
Created on 11-08-2022 07:37 PM - edited 11-08-2022 07:38 PM
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"}
Created 11-08-2022 02:00 AM
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
Created on 11-08-2022 07:37 PM - edited 11-08-2022 07:38 PM
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"}