Member since
11-08-2022
2
Posts
0
Kudos Received
1
Solution
My Accepted Solutions
| Title | Views | Posted |
|---|---|---|
| 1127 | 11-08-2022 07:37 PM |
11-08-2022
07:37 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"}
... View more