简体   繁体   中英

javascript error in nifi executeScript

I am having an issue getting the javascript script for the executeScript nifi process to work and would appreciate help with this. The goal is to pass a flowfile which will contain a json object. I need to parse this json without knowing the content/fields prior and pass this along to write it out to the flowfile that is being passed out to the next process that is MergeContent and counts the number flowfiles. Tried testing the script and got the following error:

 nifi.script.ExecuteScript - ExecuteScript[id=bd6842e9-e3a4-4d88-a59d- 
 7da1d74d109b] ExecuteScript[id=bd6842e9-e3a4-4d88-a59d-7da1d74d109b] 
 failed to process due to 
 org.apache.nifi.processor.exception.ProcessException: 
 javax.script.ScriptException: <eval>:21:17 Expected : but found value
         let value = json[key];
             ^ in <eval> at line number 21 at column number 17; rolling 
 back session: org.apache.nifi.processor.exception.ProcessException: 
 javax.script.ScriptException: <eval>:21:17 Expected : but found value

I am not very familiar with javascript so would appreciate the help.

flowFile = session.get();
if (flowFile != null) {

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 transformed_message = {};
var error = false;
var line = "ops_testQueue";

flowFile = session.write(flowFile, new StreamCallback(function 
  (inputStream, outputStream) {

    var content = IOUtils.toString(inputStream, 
  StandardCharsets.UTF_8); // message or content
    var message_content = {};
    try {
        message_content = JSON.parse(content);
        if(Array.isArray(message_content)){

    }
    Object.keys(message_content).forEach((key) => {
         var value = json[key];
         result.push(key + '=' + value);
         var jkey = "," +  "\"" + key + "\""  + '=' + value   
        });
    line = line + jkey +
            " value=" + "1"
            + " " + Date.now() * 1000000;


        // Write output content
        if (transformed_message) {
            outputStream.write(line.getBytes(StandardCharsets.UTF_8));
        }
    } catch (e) {
        error = true;
        outputStream.write(content.getBytes(StandardCharsets.UTF_8));
    }
 }));
 if (transformed_message.post_state) {
    flowFile = session.putAttribute(flowFile, "type", 
    transformed_message.type);
}

if (error) {
    session.transfer(flowFile, REL_FAILURE)
} else {
    session.transfer(flowFile, REL_SUCCESS)
}

}

EDIT:

input to executeScript:

 {"pID":"1029409411108724738",
  "contentType":"text",
  "published":"2018-08-14 16:48:23Z",
  "crawled":"2018-08-14 12:48:33-04:00",
  "ID":"765"}

output from executeScript:

ops_testQueue,"ID"=765 value=1 1534265314969999870

Am I missing something?

I saw a couple of things here:

  1. I don't know if Nashorn (Java's JS Engine) supports the full lambda syntax, I was able to get it to work by making the lambda a function (see script below).
  2. You refer to a json variable to get the value from a key, but I think you want message_content .
  3. result is not defined, so you get an error when you push to it.

Here's an edited version of your script that I got to work the way I think you want it (but please correct me if I'm wrong):

flowFile = session.get();
if (flowFile != null) {

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 transformed_message = {};
var error = false;
var line = "ops_testQueue";

flowFile = session.write(flowFile, new StreamCallback(function 
  (inputStream, outputStream) {

    var content = IOUtils.toString(inputStream, 
  StandardCharsets.UTF_8); // message or content
    var message_content = {};
    try {
        message_content = JSON.parse(content);
        if(Array.isArray(message_content)){

    }
    var jkey = "";
    Object.keys(message_content).forEach(function(key) {
         var value = message_content[key];
         //result.push(key + '=' + value);
         jkey = "," +  "\"" + key + "\""  + '=' + value   
        });
    line = line + jkey +
            " value=" + "1"
            + " " + Date.now() * 1000000;

        // Write output content
        if (transformed_message) {
            outputStream.write(line.getBytes(StandardCharsets.UTF_8));
        }
    } catch (e) {
        error = true;
        log.error(e);
        outputStream.write(content.getBytes(StandardCharsets.UTF_8));
    }
 }));
 if (transformed_message.post_state) {
    flowFile = session.putAttribute(flowFile, "type", 
    transformed_message.type);
}

if (error) {
    session.transfer(flowFile, REL_FAILURE)
} else {
    session.transfer(flowFile, REL_SUCCESS)
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM