在 Jolt 中基于属性添加新的序列列(计数器)。

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

Adding new Sequence Column(Counter) in Jolt based on a Attribute

问题

在基于属性的 Jolt 中添加新的序列列(计数器)

我正在编写一个 Jolt,根据 ProductId 项目添加一个 Id 列(应该是增量的)。一个产品可以有多个子产品,有时只有一个。我的 SubProductID 列应该始终从特定的 productId 开始,始终为 1

当前输入:

  1. [
  2. {
  3. "ProductId": "123",
  4. "ProductName": "ABC",
  5. "SubProductName": "SUB1"
  6. },
  7. {
  8. "ProductId": "123",
  9. "ProductName": "ABC",
  10. "SubProductName": "SUB2"
  11. },
  12. {
  13. "ProductId": "888",
  14. "ProductName": "DEF",
  15. "SubProductName": "DEF1"
  16. },
  17. {
  18. "ProductId": "999",
  19. "ProductName": "XYZ",
  20. "SubProductName": "RET"
  21. }
  22. ]

期望输出:

  1. [
  2. {
  3. "ProductId": "123",
  4. "ProductName": "ABC",
  5. "SubProductName": "SUB1",
  6. "SubProductIDSEQ": "1"
  7. },
  8. {
  9. "ProductId": "123",
  10. "ProductName": "ABC",
  11. "SubProductName": "SUB2",
  12. "SubProductIDSEQ": "2"
  13. },
  14. {
  15. "ProductId": "888",
  16. "ProductName": "DEF",
  17. "SubProductName": "DEF1",
  18. "SubProductIDSEQ": "1"
  19. },
  20. {
  21. "ProductId": "999",
  22. "ProductName": "XYZ",
  23. "SubProductName": "RET",
  24. "SubProductIDSEQ": "1"
  25. }
  26. ]

非常感谢任何帮助。

英文:

Adding new Sequence Column(Counter) in Jolt based on a Attribute

I am writing a Jolt to add a Id column (it should be incremental)based on the ProductId item . A product can have multiple sub products or sometimes only 1. My SubProductID column should start always start with 1 for a particular productId.

Current Input :

  1. [
  2. {
  3. "ProductId": "123",
  4. "ProductName": "ABC",
  5. "SubProductName": "SUB1"
  6. },
  7. {
  8. "ProductId": "123",
  9. "ProductName": "ABC",
  10. "SubProductName": "SUB2"
  11. },
  12. {
  13. "ProductId": "888",
  14. "ProductName": "DEF",
  15. "SubProductName": "DEF1"
  16. },
  17. {
  18. "ProductId": "999",
  19. "ProductName": "XYZ",
  20. "SubProductName": "RET"
  21. }
  22. ]

Output Expected :

  1. [
  2. {
  3. "ProductId": "123",
  4. "ProductName": "ABC",
  5. "SubProductName": "SUB1",
  6. "SubProductIDSEQ": "1"
  7. },
  8. {
  9. "ProductId": "123",
  10. "ProductName": "ABC",
  11. "SubProductName": "SUB2",
  12. "SubProductIDSEQ": "2"
  13. },
  14. {
  15. "ProductId": "888",
  16. "ProductName": "DEF",
  17. "SubProductName": "DEF1",
  18. "SubProductIDSEQ": "1"
  19. },
  20. {
  21. "ProductId": "999",
  22. "ProductName": "XYZ",
  23. "SubProductName": "RET",
  24. "SubProductIDSEQ": "1"
  25. }
  26. ]

Any help is much appreciated

答案1

得分: 2

正如我们所知,数组的索引从零开始,所以我们将通过使用 intSum 函数将它们增加 1,在按 ProductId 值对它们进行分组后。

你可以使用以下转换来获得所需的结果:

  1. [
  2. { // 按“ProductId”值对对象进行分组
  3. "operation": "shift",
  4. "spec": {
  5. "*": {
  6. "@": "@1,ProductId[]"
  7. }
  8. }
  9. },
  10. { // 使每个“ProductId”值的所有索引从零开始
  11. // 同时生成新属性“SubProductIDSEQ”
  12. "operation": "shift",
  13. "spec": {
  14. "*": {
  15. "*": {
  16. "@": "&2.&1",
  17. "$": "&2.&1.SubProductIDSEQ"
  18. }
  19. }
  20. }
  21. },
  22. { // 增加“SubProductIDSEQ”的值,使其在每个组中从1开始
  23. "operation": "modify-overwrite-beta",
  24. "spec": {
  25. "*": {
  26. "*": {
  27. "SubProductIDSEQ": "=intSum(1,@(1,&))"
  28. }
  29. }
  30. }
  31. },
  32. { // 回到JSON的原始形式
  33. "operation": "shift",
  34. "spec": {
  35. "*": {
  36. "*": ""
  37. }
  38. }
  39. }
  40. ]

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

在 Jolt 中基于属性添加新的序列列(计数器)。

英文:

As we know, the indexes of the arrays start from zero, so we'll increment them by 1 through use of intSum function after having got them grouped by the ProductId values

you can use the following transformation in order to get the desired result :

  1. [
  2. { // group the objects by "ProductId" values
  3. "operation": "shift",
  4. "spec": {
  5. "*": {
  6. "@": "@1,ProductId[]"
  7. }
  8. }
  9. },
  10. { // make all indexes starting from zero per each "ProductId" value
  11. // while generating the new attribute "SubProductIDSEQ"
  12. "operation": "shift",
  13. "spec": {
  14. "*": {
  15. "*": {
  16. "@": "&2.&1",
  17. "$": "&2.&1.SubProductIDSEQ"
  18. }
  19. }
  20. }
  21. },
  22. { // increment the values of "SubProductIDSEQ" to make it starting from 1 per each group
  23. "operation": "modify-overwrite-beta",
  24. "spec": {
  25. "*": {
  26. "*": {
  27. "SubProductIDSEQ": "=intSum(1,@(1,&))"
  28. }
  29. }
  30. }
  31. },
  32. { // back to the original form of the JSON
  33. "operation": "shift",
  34. "spec": {
  35. "*": {
  36. "*": ""
  37. }
  38. }
  39. }
  40. ]

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

在 Jolt 中基于属性添加新的序列列(计数器)。

huangapple
  • 本文由 发表于 2023年7月27日 18:43:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778951.html
匿名

发表评论

匿名网友

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

确定