【问题标题】:JOLT: "multiply" each record of an array with each record of another arrayJOLT:将一个数组的每条记录与另一个数组的每条记录“相乘”
【发布时间】:2023-01-17 17:58:32
【问题描述】:

源 JSON(示例)我尝试将每条“连接”记录与每条“测试”记录结合起来。 (我是 JOLT 和堆栈溢出方面的初学者,所以请宽容 ;-)

{
  "scen_id": 62,
  "parameters": {
    "name": "TWAMP_S1NR_VBEI",
    "duration": 1,
    "upload": 60,
    "endless_duration": true,
    "scen_id": 62
  },
  "connections": [
    {
      "synchro": false,
      "manufacturer": 6,
      "light": true,
      "conn_id": 1,
      "_id": "63051ddf26a5ce557ee2cf39",
      "index": 0
    },
    {
      "synchro": false,
      "manufacturer": 6,
      "light": true,
      "conn_id": 2,
      "_id": "63051ddf26a5ce557ee2cf38",
      "index": 1
    }
  ],
  "tests": [
    {
      "name": "TOS30",
      "test_id": 1,
      "_id": "63051ddf26a5ce557ee2cf3a"
    },
    {
      "name": "TOS31",
      "test_id": 2,
      "_id": "63051ddf26a5ce557ee2cf3a"
    }
  ]
}

我试过的 JOLT 规范

我打算先遍历其中一个数组,然后将其与第二个数组的每条记录“相乘”。

[
  {
    "operation": "shift",
    "spec": {
      "connections": {
        "*": {
          "*": "&2.[&1].&",
          "@(2,tests)": "&2.[&1].tests"
        }
      }
    }
  }
]

结果是这样的

我将整个第二个数组添加到 array1 的每个记录

{
  "connections" : [ {
    "tests" : [ {
      "name" : "TOS30",
      "test_id" : 1,
      "_id" : "63051ddf26a5ce557ee2cf3a"
    }, {
      "name" : "TOS31",
      "test_id" : 2,
      "_id" : "63051ddf26a5ce557ee2cf3a"
    } ],
    "synchro" : false,
    "manufacturer" : 6,
    "light" : true,
    "conn_id" : 1,
    "_id" : "63051ddf26a5ce557ee2cf39",
    "index" : 0
  }, {
    "tests" : [ {
      "name" : "TOS30",
      "test_id" : 1,
      "_id" : "63051ddf26a5ce557ee2cf3a"
    }, {
      "name" : "TOS31",
      "test_id" : 2,
      "_id" : "63051ddf26a5ce557ee2cf3a"
    } ],
    "synchro" : false,
    "manufacturer" : 6,
    "light" : true,
    "conn_id" : 2,
    "_id" : "63051ddf26a5ce557ee2cf38",
    "index" : 1
  } ]
}

我所期望的

{
  "connections": [
    {
      "synchro": false,
      "manufacturer": 6,
      "light": true,
      "conn_id": 1,
      "_id": "63051ddf26a5ce557ee2cf39",
      "index": 0,
      "name": "TOS30",
      "test_id": 1,
      "_id": "63051ddf26a5ce557ee2cf3a"
    },
    {
      "synchro": false,
      "manufacturer": 6,
      "light": true,
      "conn_id": 1,
      "_id": "63051ddf26a5ce557ee2cf39",
      "index": 0,
      "name": "name": "TOS3",
      "test_id": 2,
      "_id": "63051ddf26a5ce557ee2cf3a"
    },
    {
      "synchro": false,
      "manufacturer": 6,
      "light": true,
      "conn_id": 2,
      "_id": "63051ddf26a5ce557ee2cf38",
      "index": 1,
      "name": "TOS30",
      "test_id": 1,
      "_id": "63051ddf26a5ce557ee2cf3a"
    },
    {
      "synchro": false,
      "manufacturer": 6,
      "light": true,
      "conn_id": 2,
      "_id": "63051ddf26a5ce557ee2cf38",
      "index": 1,
      "name": "name": "TOS3",
      "test_id": 2,
      "_id": "63051ddf26a5ce557ee2cf3a"      
    }   
  ]
}

非常感谢每一个提示/解释!

【问题讨论】:

  • 您不能在所需的输出中有两个相同的 _id 键。
  • 谢谢@Mohammad,是的!因此,我添加了另一个“前置步骤”,在将它们合并在一起之前,我将这些键设置为唯一

标签: apache-nifi transformation combine shift jolt


【解决方案1】:

您可以使用这样的规范来展平 JSON:

[
  {
    "operation": "shift",
    "spec": {
      "connections": {
        "*": {
          "*": "&1.&",
          "@(2,tests)": "&1.tests"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "tests": {
          "*": {
            "@(2,synchro)": "&3.&1.synchro",
            "@(2,manufacturer)": "&3.&1.manufacturer",
            "@(2,light)": "&3.&1.light",
            "@(2,conn_id)": "&3.&1.conn_id",
            "@": "&3.&1"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": "&3[&2].&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

我只在 test 数组中选择了 _id 属性,因为在这种情况下预期的 JSON 值不能有重复的键。

【讨论】:

  • 亲爱的 Barbaros,非常感谢您的支持!该解决方案适合我,因为我不需要所有字段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多