英文:
How to implement if-else condition in JoltTransforms in Apache NiFi?
问题
我正在准备 jolt 规范,在其中我需要实现 if-else 条件,但我无法实现。请帮助我。
注意:
- 如果输入中包含
application-component-node对象,则打印该对象并且丢弃application-component和application(如果所有三个应用程序对象都出现在输入中)。 - 如果输入中包含
application-component对象,则打印该对象并且丢弃application对象(如果输入中有两个应用程序对象数组)。 - 如果输入中仅包含
application对象,则打印该对象。
例如:
[
{
"severity": "ERROR",
"type": "POLICY_CONTINUES_CRITICAL",
"affectedEntities": [
{
"entityType": "POLICY",
"name": "Exception Per Minute",
"entityId": 3683
},
{
"entityType": "APPLICATION",
"name": "Teashop",
"entityId": 6026
},
{
"entityType": "APPLICATION-Component",
"name": "Teashop",
"entityId": 602667
}
],
"subType": "OVERALL_APPLICATION",
"id": 92876278,
"triggeredEntity": null
}
]
预期输出:
[
{
"severity": "ERROR",
"type": "POLICY_CONTINUES_CRITICAL",
"affectedEntities": [
{
"entityType": "APPLICATION-Component",
"name": "Teashop",
"entityId": 602667
}
],
"subType": "OVERALL_APPLICATION",
"id": 92876278,
"triggeredEntity": null
}
]
在这里,预期输出是 application-component-node,并且丢弃其他两个应用程序对象数组。
三个对象数组的优先级如下:
- 优先级 1 是
application-component-node, - 优先级 2 是
application-component, - 优先级 3 是
application。
注意:三个对象数组是动态的,有时只会出现两个对象数组,有时只会出现一个对象数组。
英文:
I am preparing the jolt specification where I need to implement if-else condition but I am unable to achieve it. Please help me out.
Note :
- if
application-component-nodeobject is coming in the input,
then print that and dropapplication-componentandapplication(if all three application object coming input). - if
application-componentobject is coming in the input, then
print that and dropapplicationobject (if all two
application object arrays coming input). - if
applicationobject is only coming in the input, then print
that.
For Example:
[
{
"severity": "ERROR",
"type": "POLICY_CONTINUES_CRITICAL",
"affectedEntities": [
{
"entityType": "POLICY",
"name": "Exception Per Minute",
"entityId": 3683
},
{
"entityType": "APPLICATION",
"name": "Teashop",
"entityId": 6026
},
{
"entityType": "APPLICATION-Component",
"name": "Teashop",
"entityId": 602667
}
],
"subType": "OVERALL_APPLICATION",
"id": 92876278,
"triggeredEntity": null
}
]
Expected output:
[
{
"severity": "ERROR",
"type": "POLICY_CONTINUES_CRITICAL",
"affectedEntities": [
{
"entityType": "APPLICATION-Component",
"name": "Teashop",
"entityId": 602667
}
],
"subType": "OVERALL_APPLICATION",
"id": 92876278,
"triggeredEntity": null
}
]
Here the expected output is application-component-node and drop the other two application object arrays
The priority among three object arrays is as follows:
- Priority 1 is for
application-component-nodeand, - Priority 2 is for
application-componentand, - Priority 3 is for
application
Note : Three object arrays are dynamic and they might come sometime only two object arrays come and sometimes only one object array may come.
答案1
得分: 1
你可以在modify规范中使用一个名为lastElement的函数,例如:
[
{
"operation": "sort"
},
{
"operation": "shift",
"spec": {
"*": {
"*": "others.&",
"app*": {
"@": "app[].&"
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"app": "=lastElement(@(1,&))"
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[0].&"
}
}
}
]
编辑:你还可以根据你最后的编辑,在modify规范中使用名为firstElement的函数,采用以下变换:
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&",
"aff*": {
"*": {
"*": "&2.@(1,entityType).&" // 使用 entityType 键重建子对象
}
}
}
}
},
{ // 用于排序目的
"operation": "shift",
"spec": {
"*": "&",
"affectedEntities": {
"APPLICATION-Component-Node": "&1[]",
"APPLICATION-Component": "&1[]",
"APPLICATION": "&1[]",
"POLICY": "&1[]"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"aff*": "=firstElement(@(1,&))"
}
}
]
在最后的shift变换中,列表用于按给定的实体类型按其呈现的大小写进行排序。
在网站http://jolt-demo.appspot.com/上的demo如下:
英文:
You can use a lastElement function in a modify spec such as
[
{
"operation": "sort"
},
{
"operation": "shift",
"spec": {
"*": {
"*": "others.&",
"app*": {
"@": "app[].&"
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"app": "=lastElement(@(1,&))"
}
},
{
"operation": "shift",
"spec": {
"*": {
"*": "[0].&"
}
}
}
]
Edit : You can alternatively use the following transformation with
firstElement function in a modify spec based on your last edit :
[
{
"operation": "shift",
"spec": {
"*": {
"*": "&",
"aff*": {
"*": {
"*": "&2.@(1,entityType).&" // reconstruct subobjects with entityType keys
}
}
}
}
},
{ // used for sorting purposes
"operation": "shift",
"spec": {
"*": "&",
"affectedEntities": {
"APPLICATION-Component-Node": "&1[]",
"APPLICATION-Component": "&1[]",
"APPLICATION": "&1[]",
"POLICY": "&1[]"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"aff*": "=firstElement(@(1,&))"
}
}
]
where the case-sensitivity of the entityTypes matter. The listing within the last shift transformation is used to order by those given entity types as in their presented letter cases.
the demo on the site http://jolt-demo.appspot.com/ is :
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论