在 Jolt 中为多列应用基数

Applying cardinality for multiple columns in Jolt

我正在尝试为以下数据应用 Jolt

输入:

[
  {
    "id": "500",
    "code": "abc",
    "date": "2020-10-10",
    "category": 1,
    "amount": 100,
    "result": 0
  },
  {
    "id": "500",
    "code": "abc",
    "date": "2020-10-10",
    "category": 2,
    "amount": 200,
    "result": 1
  }
]

使用震动:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "id": "@(1,id).id",
        "code": "@(1,id).code",
        "date": "@(1,id).group1.date",
        "category": "@(1,id).group1.group2[&1].category"
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "id": "ONE"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

当前输出:

{
  "id": "500",
  "code": [
    "abc",
    "abc"
  ],
  "group1": {
    "date": [
      "2020-10-10",
      "2020-10-10"
    ],
    "group2": [
      {
        "category": 1
      },
      {
        "category": 2
      }
    ]
  }
}

预期:

{
  "id": "500",
  "code": "abc",
  "group1": {
    "date": "2020-10-10",
    "group2": [
      {
        "category": 1
      },
      {
        "category": 2
      }
    ]
  }
}

如果我将代码和日期列保留为基数,就可以了。但在我的用例中,有多个这样的列要添加。有没有更好的方法来处理这种情况?

您应该添加每个添加的节点并使用"*"通配符来表示rest内的属性cardinality转换如

{
  "operation": "cardinality",
  "spec": {
    "*": {
      "*": "ONE",
      "group1": {
        "*": "ONE",
        "group2": "MANY"
      }
    }
  }
}

其中 "group2": "MANY" 将使 group2 被排除在外,仅提取相应列表的第一个元素。

网站上的 演示 http://jolt-demo.appspot.com/ :