英文:
How to consider null values in jolt spec in NIFI?
问题
[
{
"Agent": "A1",
"loc_id": "L1",
"Company": {
"Hyundai": [
{
"Verna": [
{
"2018": []
}
]
},
{
"Creta": [
{}
]
},
{
"Aura": [
{}
]
}
]
}
},
{
"Agent": "A2",
"loc_id": "L1",
"Company": {
"Toyota": [
{
"Fortuner": [
{
"2020": []
}
]
}
]
}
},
{
"Agent": "A3",
"loc_id": "L1",
"Company": {
"BMW": [
{}
]
}
}
]
英文:
For the given CSV data, the jolt spec is not considering the null values.
I have 2 points to consider.
- if submodel1 is empty then it should atleast print till model1.
- if model1 is empty then it should print till company.(Agent A3 case)
Here null will be empty value in CSV
CSV DATA
[
{
"Agent": "A1",
"Location": "L1",
"Company": "Hyundai",
"Model1": "Verna",
"Sub-Model1": "2018"
},
{
"Agent": "A1",
"Location": "L1",
"Company": "Hyundai",
"Model1": "Creta",
"Sub-Model1": null
},
{
"Agent": "A1",
"Location": "L1",
"Company": "Hyundai",
"Model1": "Aura",
"Sub-Model1":null
},
{
"Agent": "A2",
"Location": "L1",
"Company": "Toyota",
"Model1": "Fortuner",
"Sub-Model1": "2020"
},
{
"Agent": "A3",
"Location": "L1",
"Company": "BMW",
"Model1": null,
"Sub-Model1": null
}
]
Jolt SPec:
[
{
"operation": "shift",
"spec": {
"*": {
"@(0,Agent)": "@(1,Agent).Agent",
"@(0,Location)": "@(1,Agent).loc_id",
"Sub-Model1": "@(1,Agent).Company.@(1,Company).@(1,Model1).@(1,Sub-Model1)"
}
}
},
{
"operation": "cardinality",
"spec": {
"*": { // A1, A2
"loc_id": "ONE",
"Agent": "ONE"
}
}
},
{
"operation": "shift",
"spec": {
"*": { // A1, A2
"*": "[#2].&",
"Company": {
"*": { // Hyundai
"*": { // Verna
"*": { // 2018
"@": "[#6].&4.&3[#3].&2.[#1].&"
}
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": []
}
}
}
}
}
}
}
}
]
Output: This is json I'm getting but I want to print the available data.
For creta, AURA nothing has printed and BMW is not printed also
[
{
"Agent": "A1",
"loc_id": "L1",
"Company": {
"Hyundai": [
{
"Verna": [
{
"2018": []
}
]
}
]
}
},
{
"Agent": "A2",
"loc_id": "L1",
"Company": {
"Toyota": [
{
"Fortuner": [
{
"2020": []
}
]
}
]
}
},
{
"Agent": "A3",
"loc_id": "L1"
}
]
Expected output
[
{
"Agent": "A1",
"loc_id": "L1",
"Company": {
"Hyundai": [
{
"Verna": [
{
"2018": []
}
]
},
{
"Creta": [
{}
]
},
{
"Aura": [
{}
]
}
]
}
},
{
"Agent": "A2",
"loc_id": "L1",
"Company": {
"Toyota": [
{
"Fortuner": [
{
"2020": []
}
]
}
]
}
},
{
"Agent": "A3",
"loc_id": "L1",
"Company": {
"BMW": [
{}
]
}
}
]
答案1
得分: 1
你可以在modify转换中使用notNull函数的else case,并根据它们各自的**Agent
**值将属性分割成子对象,如下所示:
[
{ // 对“Model1”和“Sub-Model1”属性的空元素应用转换
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*odel1": ["=notNull", " "]
}
}
},
{ // 按照“Agent”值对对象进行分组
"operation": "shift",
"spec": {
"*": {
"Agent": "@1,Agent.&",
"Location": "@1,Agent.loc_id",
"# ": "@1,Agent.@1,Company[0].@1,Model1[].@1,Sub-Model1"
}
}
},
{ // 摒弃对象的键
"operation": "shift",
"spec": {
"*": {
"@": ""
}
}
},
{ // 摒弃具有用于转换的“ ”键的属性,作为else case
"operation": "remove",
"spec": {
"*": {
"*": {
"*": {
" ": "",
"*": {
"*": {
" ": ""
}
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": ["= ", []]
}
}
}
}
}
}
},
{ // 将重复组件的数量减少到一个
"operation": "cardinality",
"spec": {
"*": {
"Agent": "ONE",
"loc_id": "ONE"
}
}
}
]
英文:
You can use else case of the notNull function within a modify transformation, and partition the attributes into sub-objects by their respective Agent
values such as
[
{ // apply conversion for the null elements of "Model1" and "Sub-Model1" attributes
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*odel1": ["=notNull", " "] // the values are overwritten provided the attributes have null values
}
}
},
{ // group the objects by their "Agent" values
"operation": "shift",
"spec": {
"*": {
"Agent": "@1,Agent.&",
"Location": "@1,Agent.loc_id",
"# ": "@1,Agent.@1,Company[0].@1,Model1[].@1,Sub-Model1"
}
}
},
{ // get rid of the keys of the objects
"operation": "shift",
"spec": {
"*": {
"@": ""
}
}
},
{ // get rid of the attributes with " " keys which had been used above for transformation as the else case
"operation": "remove",
"spec": {
"*": {
"*": {
"*": {
" ": "",
"*": {
"*": {
" ": ""
}
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"*": {
"*": {
"*": {
"*": {
"*": ["= ", []]
}
}
}
}
}
}
},
{ // reduce the number of the repeating components to one
"operation": "cardinality",
"spec": {
"*": {
"Agent": "ONE",
"loc_id": "ONE"
}
}
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论