Member since
11-08-2022
2
Posts
0
Kudos Received
1
Solution
My Accepted Solutions
Title | Views | Posted |
---|---|---|
480 | 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
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??
... View more
Labels:
- Labels:
-
Apache NiFi