NiFi:JoltTransformJSON 规范 [英] NiFi: JoltTransformJSON specification
问题描述
我有以下 JSON:
<代码>{结果":[{客户客户":{资源名称":customers/7876562723/customerClients/8506630423",clientCustomer":customers/8506630423",隐藏":假,级别":1",经理":假,描述性名称":宝马",id":85061423"}},{客户客户":{资源名称":customers/7876562723/customerClients/6736523142",clientCustomer":customers/6736523142",隐藏":假,级别":1",经理":真的,描述性名称":梅赛德斯",id":67363142"}}}],fieldMask":customerClient.clientCustomer,customerClient.hidden,customerClient.level,customerClient.descriptiveName,customerClient.id,customerClient.manager"}
我想要的:
- 删除
fieldMask
参数,所以我不需要结果作为数组. - 删除
results
和customerCient
.所以我的 JSON 应该是这样的:
<代码>{资源名称":customers/7876562723/customerClients/8506630423",clientCustomer":customers/8506630423",隐藏":假,级别":1",经理":假,描述性名称":宝马",id":85061423"},{资源名称":customers/7876562723/customerClients/6736523142",clientCustomer":customers/6736523142",隐藏":假,级别":1",经理":真的,描述性名称":梅赛德斯",id":67363142"}
要删除results
,我使用带有拆分值的SplitJson
:$.results
.现在我坚持使用 JOLT 规范修改它.如何仅使用 JoltTransformJSON
执行上述操作?
好吧,所以我相信 JOLT 只能输出一个对象,因此在一个 JOLT 中将数组的每个元素转换为唯一的对象是不可能的.但是,您无需将 FlowFile 数据放入 Attributes 即可获得大部分方法.
我们可以:
- 删除
fieldMask
- 移除
results
数组级别
使用它来测试 JOLT:https://jolt-demo.appspot.com/#开始
假设您的 JSON 实际上有效,那么:
<代码>{结果":[{客户客户":{资源名称":customers/7876562723/customerClients/8506630423",clientCustomer":customers/8506630423",隐藏":假,级别":1",经理":假,描述性名称":宝马",id":85061423"}},{客户客户":{资源名称":customers/7876562723/customerClients/6736523142",clientCustomer":customers/6736523142",隐藏":假,级别":1",经理":真的,描述性名称":梅赛德斯",id":67363142"}}],fieldMask":customerClient.clientCustomer,customerClient.hidden,customerClient.level,customerClient.descriptiveName,customerClient.id,customerClient.manager"}
你可以使用这个 JOLT 规范:
<预><代码>[{操作":删除",规格":{字段掩码":"}},{操作":移位",规格":{结果":{*":{customerClient":customerClient-&1"}}}}]结果如下:
<代码>{customerClient-0":{资源名称":客户/7876562723/customerClients/8506630423",客户客户":客户/8506630423",隐藏": 错误的,级别":1",经理": 错误的,描述性名称":宝马",身份证":85061423"},customerClient-1":{资源名称":客户/7876562723/customerClients/6736523142",客户客户":客户/6736523142",隐藏": 错误的,级别":1",经理": 真的,描述性名称":梅赛德斯",身份证":67363142"}}
因此,您可以仅使用 JOLT 将 JSON 转换为平面结构,然后您可以使用 SplitJSON 分解每个对象(如果需要).
您应该考虑使用 Records 而不是 SplitJSON,这可能会更有效.
阅读记录:
- https://blogs.apache.org/nifi/entry/record-oriented-data-with-nifi
- https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.12.1/org.apache.nifi.processors.standard.UpdateRecord/index.html
- https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-record-serialization-services-nar/1.12.1/org.apache.nifi.json.JsonTreeReader/index.html
另一个有趣的选择可能是 ScriptedTransformRecord
I have following JSON:
{
"results": [
{
"customerClient": {
"resourceName": "customers/7876562723/customerClients/8506630423",
"clientCustomer": "customers/8506630423",
"hidden": false,
"level": "1",
"manager": false,
"descriptiveName": "BMW",
"id": "85061423"
}
},
{
"customerClient": {
"resourceName": "customers/7876562723/customerClients/6736523142",
"clientCustomer": "customers/6736523142",
"hidden": false,
"level": "1",
"manager": true,
"descriptiveName": "Mercedes",
"id": "67363142"
}
}
}
],
"fieldMask": "customerClient.clientCustomer,customerClient.hidden,customerClient.level,customerClient.descriptiveName,customerClient.id,customerClient.manager"
}
What i want:
- remove
fieldMask
parameter, so i don't need results as array. - remove
results
andcustomerCient
. So my JSON should look like this:
{
"resourceName": "customers/7876562723/customerClients/8506630423",
"clientCustomer": "customers/8506630423",
"hidden": false,
"level": "1",
"manager": false,
"descriptiveName": "BMW",
"id": "85061423"
},
{
"resourceName": "customers/7876562723/customerClients/6736523142",
"clientCustomer": "customers/6736523142",
"hidden": false,
"level": "1",
"manager": true,
"descriptiveName": "Mercedes",
"id": "67363142"
}
To remove results
i'm using SplitJson
with split value: $.results
. And now i'm stuck at modifying it with JOLT specification.
How can I do described above actions with only JoltTransformJSON
?
Ok, so I believe JOLT can only output one object, so transforming each element of the array to unique objects wouldn't be possible in one JOLT. However, you can get most of the way there without putting FlowFile data in to Attributes.
We can:
- delete
fieldMask
- Remove the
results
array level
Use this to test out JOLTs: https://jolt-demo.appspot.com/#inception
Assuming your JSON is actually valid, so:
{
"results": [
{
"customerClient": {
"resourceName": "customers/7876562723/customerClients/8506630423",
"clientCustomer": "customers/8506630423",
"hidden": false,
"level": "1",
"manager": false,
"descriptiveName": "BMW",
"id": "85061423"
}
},
{
"customerClient": {
"resourceName": "customers/7876562723/customerClients/6736523142",
"clientCustomer": "customers/6736523142",
"hidden": false,
"level": "1",
"manager": true,
"descriptiveName": "Mercedes",
"id": "67363142"
}
}
],
"fieldMask": "customerClient.clientCustomer,customerClient.hidden,customerClient.level,customerClient.descriptiveName,customerClient.id,customerClient.manager"
}
You could use this JOLT spec:
[
{
"operation": "remove",
"spec": {
"fieldMask": ""
}
},
{
"operation": "shift",
"spec": {
"results": {
"*": {
"customerClient": "customerClient-&1"
}
}
}
}
]
Which gives you following result:
{
"customerClient-0" : {
"resourceName" : "customers/7876562723/customerClients/8506630423",
"clientCustomer" : "customers/8506630423",
"hidden" : false,
"level" : "1",
"manager" : false,
"descriptiveName" : "BMW",
"id" : "85061423"
},
"customerClient-1" : {
"resourceName" : "customers/7876562723/customerClients/6736523142",
"clientCustomer" : "customers/6736523142",
"hidden" : false,
"level" : "1",
"manager" : true,
"descriptiveName" : "Mercedes",
"id" : "67363142"
}
}
So you can transform your JSON to a flat structure using just the JOLT, then you could use SplitJSON to break each object up (if needed).
You should consider using Records instead of SplitJSON, this would probably be more efficient.
Read about records:
- https://blogs.apache.org/nifi/entry/record-oriented-data-with-nifi
- https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.12.1/org.apache.nifi.processors.standard.UpdateRecord/index.html
- https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-record-serialization-services-nar/1.12.1/org.apache.nifi.json.JsonTreeReader/index.html
Another interesting alternative could be ScriptedTransformRecord
这篇关于NiFi:JoltTransformJSON 规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!