Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

In apache nifi Execute script is not working when Result per flow file in undefined ?

In apache nifi Execute script is not working when Result per flow file in undefined ?

Explorer

I am creating a Job in Apache NIFI which loads the data from mongodb in json format and then convert it to csv. I am using execute script to manipulate some data in json. So the data is coming from GetMongo processor it then it passes to the Executescript. I am using Javascript to manipulate the data.
Problem: When I set the Result per flow file property to default it throws a error that certain property of flowfile is undefined. But if I changed it to 1 or more it runs successfully.
Ex. in the below script it throws cannot read property packageName from undefined.

This is my java script

var flowFile = session.get();
if (flowFile != null) {
  var newFlowFile = [];
  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 index = 0;
    flowFile = session.write(flowFile,new StreamCallback(function(inputStream,outputStream){
        var text = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
var obj = JSON.parse(JSON.stringify(text));
    for(var i in obj)
{
                    newFlowFile[i] = obj[i];
                    newFlowFile[i]["id"] = obj[i]._id;
                   newFlowFile[i]["package_name"] = obj[i].interceptionInfos.packagename || null;
                    newFlowFile[i]["package_version"] = obj[i].interceptionInfos.packageVersionName || null;
                    newFlowFile[i]["sdk_version"] = obj[i].interceptionInfos.sdkVersionName || null;
                    newFlowFile[i]["app_id"] = obj[i].appId || null;
                    delete newFlowFile[i]._id;
                    delete newFlowFile[i].interceptionInfos;
}
                    newFlowFile = JSON.parse(JSON.stringify(newFlowFile));
       outputStream.write(JSON.stringify(newFlowFile).getBytes(StandardCharsets.UTF_8)) 
  }));
  flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').split('.')[0]+'_translated.json')
  session.transfer(flowFile, REL_SUCCESS)
}

This is the screenshot of GetMongoProcessor.

64988-im.png

Remember if I set the property result per flow file to 1 or more it runs successfully.
Any Idea why this is happening

Don't have an account?
Coming from Hortonworks? Activate your account here