Nifi executeScript中的javascript错误

时间:2018-08-13 14:39:43

标签: javascript apache-nifi

我在获取JavaScript脚本以使executeScript nifi进程正常工作时遇到问题,希望对此有所帮助。目标是传递将包含json对象的流文件。我需要在不知道内容/字段的情况下解析此json,并将其传递给流文件,该流文件将被传递到下一个流程,即MergeContent并计算流文件的数量。 尝试测试脚本并得到以下错误:

 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

我对javascript不太熟悉,希望能获得帮助。

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)
}

}

编辑:

executeScript的输入:

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

executeScript的输出:

ops_testQueue,"ID"=765 value=1 1534265314969999870

我想念什么吗?

1 个答案:

答案 0 :(得分:2)

我在这里看到了几件事:

  1. 我不知道Nashorn(Java的JS引擎)是否支持完整的lambda 语法,通过使lambda函数可以使它正常工作 (请参见下面的脚本)。
  2. 您引用一个json变量以从键中获取值,但我认为您想要message_content
  3. result未定义,因此将其推入时会出错。

这是您脚本的编辑版本,我可以按照我认为您想要的方式工作(但是如果我错了,请更正我):

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)
}
}