Jolt transform – 如何使用字段值重命名字段?

huangapple go评论61阅读模式
英文:

Jolt transform - how to rename field with field value?

问题

我想知道是否有办法使用JOLT转换将JSON属性的值与现有属性名称连接起来来重命名字段。

假设我们有以下输入

{
  "auth_id": "0000-0000-0000",
  "read_time": "2022-01-10T00:00:00.0",
  "src_name": "REQ-A001",
  "reading_a": "150.18",
  "reading_b": "12.10",
  "reading_c": "3.00",
  "note": 1
}

我期望的结果是将auth_idread_time的字段值连接到所有现有字段名称之前,使用冒号(:)作为分隔符,结果如下:

期望的输出

{
  "0000-0000-0000:2022-01-10T00:00:00.0:auth_id": "0000-0000-0000-0000-0000",
  "0000-0000-0000:2022-01-10T00:00:00.0:read_time": "2022-01-10T00:00:00.0",
  "0000-0000-0000:2022-01-10T00:00:00.0:src_name": "REQ-A001",
  "0000-0000-0000:2022-01-10T00:00:00.0:reading_a": "150.18",
  "0000-0000-0000:2022-01-10T00:00:00.0:reading_b": "12.10",
  "0000-0000-0000:2022-01-10T00:00:00.0:reading_c": "3.00",
  "0000-0000-0000:2022-01-10T00:00:00.0:note": 1
}

到目前为止,我参考了以下内容:

并创建了以下JOLT规范

[
  {
    "operation": "modify-default-beta",
    "spec": {
      // 连接字段值auth_id和read_time,使用冒号作为分隔符
      "ukey": "=concat(@(1,auth_id),':',@(1,read_time))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        // 在字段名称之前连接字段值
        "@": "@(2,ukey):&"
      }
    }
  }
]

但我得到了以下输出:

{
  "0000-0000-0000:2022-01-10T00:00:00.0": {
    ":auth_id": "0000-0000-0000",
    ":read_time": "2022-01-10T00:00:00.0",
    ":src_name": "REQ-A001",
    ":reading_a": "150.18",
    ":reading_b": "12.10",
    ":reading_c": "3.00",
    ":note": 1
  }
}

我期望将JOLT规范应用于Nifi中的JoltTransformJSON处理器。

非常感谢任何帮助或指导!

英文:

I'm wondering if there is any way to rename the field by concatenating the values of JSON attributes into existing attribute names using JOLT transformation.

Suppose we have input here:

{
  "auth_id": "0000-0000-0000",
  "read_time": "2022-01-10T00:00:00.0",
  "src_name": "REQ-A001",
  "reading_a": "150.18",
  "reading_b": "12.10",
  "reading_c": "3.00",
  "note": 1
}

What I expect is to concatenate field values from auth_id and read_time before all existing field name, with a colon (:) as separator and the result would become:

Expected:

{
  "0000-0000-0000:2022-01-10T00:00:00.0:auth_id": "0000-0000-0000-0000-0000",
  "0000-0000-0000:2022-01-10T00:00:00.0:read_time": "2022-01-10T00:00:00.0",
  "0000-0000-0000:2022-01-10T00:00:00.0:src_name": "REQ-A001",
  "0000-0000-0000:2022-01-10T00:00:00.0:reading_a": "150.18",
  "0000-0000-0000:2022-01-10T00:00:00.0:reading_b": "12.10",
  "0000-0000-0000:2022-01-10T00:00:00.0:reading_c": "3.00",
  "0000-0000-0000:2022-01-10T00:00:00.0:note": 1
}

So far I've referred to

and come up with a JOLT spec:

[
  {
    "operation": "modify-default-beta",
    "spec": {
      // concatenate field value auth_id and read_time, while using ":" as separator
      "ukey": "=concat(@(1,auth_id),':',@(1,read_time))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        // concatenate field values before field name
        "@": "@(2,ukey):&"
      }
    }
  }
]

But I got this output:

{
  "0000-0000-0000:2022-01-10T00:00:00.0": {
    ":auth_id": "0000-0000-0000",
    ":read_time": "2022-01-10T00:00:00.0",
    ":src_name": "REQ-A001",
    ":reading_a": "150.18",
    ":reading_b": "12.10",
    ":reading_c": "3.00",
    ":note": 1
  }
}

I expect to use the JOLT spec in the JoltTransformJSON processor in Nifi.

Any help or guidance is much appreciated!

答案1

得分: 1

以下是翻译好的部分:

You can use the following transformation spec

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "ukey": "=concat(@(1,auth_id),':',@(1,read_time),':')"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "@(0)" // reverse key-value pairs
      },
      "ukey": "&"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=concat(@(1,ukey),@(1,&))"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "ukey": ""
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "@(0)" // reverse key-value pairs back
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*note": "=toInteger"
    }
  }
]

在网站http://jolt-demo.appspot.com/上的 demo1 是 :

Jolt transform – 如何使用字段值重命名字段?

备选选项,它产生相同的结果,使用了已经存在的 modify 转换后的连续 shift 转换规格,如下所示

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "ukey": "=concat(@(1,auth_id),':',@(1,read_time))"
    }
  },
  { // nest the attributes under common object with "ukey" key
    "operation": "shift",
    "spec": {
      "*": {
        "@": "@(2,ukey).&"
      }
    }
  },
  {// concatenate upper object and one level inner object keys for the attributes except for "ukey"
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@": "&2:&1" // concatenation 
        },
        "ukey": {      // exception case
          "*": {
            "*": ""
          }
        }
      }
    }
  }
]

在网站http://jolt-demo.appspot.com/上的 demo2 是 :

Jolt transform – 如何使用字段值重命名字段?

英文:

You can use the following transformation spec

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "ukey": "=concat(@(1,auth_id),':',@(1,read_time),':')"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "@(0)" // reverse key-value pairs
      },
      "ukey": "&"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=concat(@(1,ukey),@(1,&))"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "ukey": ""
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "@(0)" // reverse key-value pairs back
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*note": "=toInteger"
    }
  }
]

the demo1 on the site http://jolt-demo.appspot.com/ is :

Jolt transform – 如何使用字段值重命名字段?

the alternative option, which gives the same result , uses consecutive shift transformation specs after having used the modify transformation you already have, as follows

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "ukey": "=concat(@(1,auth_id),':',@(1,read_time))"
    }
  },
  { // nest the attributes under common object with "ukey" key
    "operation": "shift",
    "spec": {
      "*": {
        "@": "@(2,ukey).&"
      }
    }
  },
  {// concatenate upper object and one level inner object keys for the attributes except for "ukey"
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@": "&2:&1" // concatenation 
        },
        "ukey": {      // exception case
          "*": {
            "*": ""
          }
        }
      }
    }
  }
]

the demo2 on the site http://jolt-demo.appspot.com/ is :

Jolt transform – 如何使用字段值重命名字段?

答案2

得分: 1

以下是翻译好的部分:

你可以使用这个更短的规范:

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

或者你可以使用另一种方法,就像下面这样:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@(2,auth_id)": "keys[#2]",
        "@(2,read_time)": "keys[#2]",
        "$": "keys[#2]",
        "@": "values"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "keys": {
        "*": "=join(':',@0)"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "values": {
        "*": "@(2,keys[#1])"
      }
    }
  }
]

1, Shift操作 分别创建keysvalues

2, Modify操作:连接所有的keys

3, Shift操作 创建我们期望的输出。

注意: 请分别运行每个规范以更好地理解这段代码。

英文:

You can use this shorter spec:

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

Jolt transform – 如何使用字段值重命名字段?

Or you can use another way like below:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@(2,auth_id)": "keys[#2]",
        "@(2,read_time)": "keys[#2]",
        "$": "keys[#2]",
        "@": "values"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "keys": {
        "*": "=join(':',@0)"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "values": {
        "*": "@(2,keys[#1])"
      }
    }
  }
]

1, Shift operation Create keys and values separately.

2, Modify operation Concate all keys with :.

3, Shift operation Create our desired output.

Note: Please run each spec separately to understand this code better.

huangapple
  • 本文由 发表于 2023年3月8日 19:24:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定