英文:
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
正确的方法是如何将属性传递给ExecuteScript处理器?
更新:见下文
因此,当我尝试弄清楚时,我正在处理以下内容。
- 读取JSON文件
- 使用EvaluateJSONPath设置一些属性
- 在这里,我需要合并一些属性,并希望使用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.
- Read in a JSON file
- Set some attributes with EvaluateJSONPath
- 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("someAttribute");
// Set the value as a new Attribute in the flowFile
session.putAttribute(flowFile, "NewAttribute", argFoo);
}
I've also tried things like
var flowFile = session.get();
if(flowFile){
var argFoo = flowFile.getAttribute("someAttribute");
// Create a new flowFile
var newFlowFile = session.create(flowFile);
// Set the value as a new Attribute in the flowFile
session.putAttribute(newFlowFile, "NewAttribute", 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
以下是翻译好的部分:
检查这些网站:
- https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-scripting-nar/1.20.0/org.apache.nifi.processors.script.ExecuteScript/additionalDetails.html
- https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-1/ta-p/248922
- https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018
- https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-3/ta-p/249148
- https://github.com/mkalika/nifi-executescript-samples
如果你只想添加一个新属性,你不需要创建一个新的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:
- https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-scripting-nar/1.20.0/org.apache.nifi.processors.script.ExecuteScript/additionalDetails.html
- https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-1/ta-p/248922
- https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-2/ta-p/249018
- https://community.cloudera.com/t5/Community-Articles/ExecuteScript-Cookbook-part-3/ta-p/249148
- https://github.com/mkalika/nifi-executescript-samples
You don't have to create new a FlowFile, if you want only to add a new attribute.
flowFile = session.get();
if (flowFile != null) {
// Get attributes
var greeting = flowFile.getAttribute("greeting");
var message = greeting + ", Script!";
// Set single attribute
flowFile = session.putAttribute(flowFile, "message", message);
// Set multiple attributes
flowFile = session.putAllAttributes(flowFile, {
"attribute.one": "true",
"attribute.two": "2"
});
session.transfer(flowFile, REL_SUCCESS)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论