NiFi如何将属性传递给executeScript处理器

huangapple go评论54阅读模式
英文:

NiFi how do I pass an attribute to the executeScript processor

问题

流文件使用evaluateJsonPath来提取值并设置我的属性。我需要将一些属性传递到一个JavaScript函数中,该函数位于ExecuteScript处理器中。设置是为ECMAScript,JS代码位于Script Body中。

所以,例如,如果我的属性是A、B、C,而我的函数是foo(arg){}

我如何调用函数foo(A)?

我尝试将其放在Script Body的末尾,在声明函数foo之后

foo(A);
foo(${A});

但这一直失败,我无法找到任何关于如何将值传递给函数调用的示例。我要么得到"A is not defined in ",要么得到"Expects a , and got a {"。

正确的方法是如何将属性传递给ExecuteScript处理器?

更新:见下文

因此,当我尝试弄清楚时,我正在处理以下内容。

  1. 读取JSON文件
  2. 使用EvaluateJSONPath设置一些属性
  3. 在这里,我需要合并一些属性,并希望使用ExecuteScript来运行一些JavaScript

JAVASCRIPT

var flowFile = session.get();
if(flowFile){
  var argFoo = flowFile.getAttribute("someAttribute");
  
  // 将值设置为flowFile中的新属性
  session.putAttribute(flowFile, "NewAttribute", argFoo);
}

我还尝试过类似于

var flowFile = session.get();
if(flowFile){
  var argFoo = flowFile.getAttribute("someAttribute");

  // 创建一个新的flowFile
  var newFlowFile = session.create(flowFile);
  // 将值设置为新的flowFile中的属性
  session.putAttribute(newFlowFile, "NewAttribute", argFoo);
}

我在盲目猜测如何使其工作。

有人能指导我如何在ExecuteScript处理器中使用JavaScript吗?

最新的错误是"此FlowFile未在此会话中创建,并且未通过ProcessSession.transfer()传递到任何关系”。

英文:

The flowfile uses evaluateJsonPath in order to extract values and setup my Attributes. I need to pass some of the attributes into a JavaScript function which I have in a ExecuteScript processor. The setting is for ECMAScript and the JS code is in the Script Body.

So, as an example if my attributes are A, B, C and my function is foo(arg){}

How do I call the function foo(A)?

I have tried putting at the end of the Script Body, after the declaration of my function foo

foo(A);
foo(${A});

But this keeps failing and I am not able to find any examples on how to pass in the value to the function call. I get either a "A is not defined in <eval>" or Expects a , and got a {.

What is the proper way to pass an Attribute to the ExecuteScript processor?

UPDATED: SEE BELOW

So as I'm trying to figure this out here is what I'm dealing with.

  1. Read in a JSON file
  2. Set some attributes with EvaluateJSONPath
  3. HERE I NEED TO merge some attributes and want to use the ExecuteScript to run some JavaScript

JAVASCRIPT

var flowFile = session.get();
if(flowFile){
  var argFoo = flowFile.getAttribute(&quot;someAttribute&quot;);
  
  // Set the value as a new Attribute in the flowFile
  session.putAttribute(flowFile, &quot;NewAttribute&quot;, argFoo);
}

I've also tried things like

var flowFile = session.get();
if(flowFile){
  var argFoo = flowFile.getAttribute(&quot;someAttribute&quot;);

  // Create a new flowFile
  var newFlowFile = session.create(flowFile);
  // Set the value as a new Attribute in the flowFile
  session.putAttribute(newFlowFile, &quot;NewAttribute&quot;, argFoo);
}

I'm blindly guessing how to get this to work.

Can someone point me in the right direction here on how to use JavaScript within this ExecuteScript processor?

The latest error is "This FlowFile was not created in this session and was not transferred to any Relationship via ProcessSession.transfer()"

答案1

得分: 1

以下是翻译好的部分:

检查这些网站:

如果你只想添加一个新属性,你不需要创建一个新的FlowFile。

示例

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

    // 获取属性
    var greeting = flowFile.getAttribute("greeting");
    var message = greeting + ", Script!";

    // 设置单个属性
    flowFile = session.putAttribute(flowFile, "message", message);

    // 设置多个属性
    flowFile = session.putAllAttributes(flowFile, {
        "attribute.one": "true",
        "attribute.two": "2"
    });

    session.transfer(flowFile, REL_SUCCESS)
}
英文:

Check these sites:

You don't have to create new a FlowFile, if you want only to add a new attribute.

Example

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

    // Get attributes
    var greeting = flowFile.getAttribute(&quot;greeting&quot;);
    var message = greeting + &quot;, Script!&quot;;

    // Set single attribute
    flowFile = session.putAttribute(flowFile, &quot;message&quot;, message);

    // Set multiple attributes
    flowFile = session.putAllAttributes(flowFile, {
        &quot;attribute.one&quot;: &quot;true&quot;,
        &quot;attribute.two&quot;: &quot;2&quot;
    });

    session.transfer(flowFile, REL_SUCCESS)
}

huangapple
  • 本文由 发表于 2023年2月14日 21:33:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定