在一个步进函数中筛选输入数组并重定向到不同的状态

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

Filtering input array in a Step Function and redirect to different states

问题

对于给定的状态输入,是否可以将“FAILS”重定向到一个状态,将“SUCCESSES”重定向到另一个状态?

如果所有的项目都是“SUCCESS”,那么Choice状态将转到“全部成功”状态,如果至少有一项是“FAILED”,那么Choice状态将转到“部分成功”状态。如果可以将失败的项目传递到“部分成功”状态,那将是不错的,但不是必需的。

英文:

Given the following input for a State, is it possible to redirect the FAILS to one state and the SUCCESSES to another?

[
  {
    "Status": "SUCCESS",
    "Stack": "metadata"
  },
  {
    "Status": "FAILED",
    "Stack": "metadata-roles"
  },
  {
    "Status": "FAILED",
    "Stack": "master-storage"
  },
  {
    "Status": "SUCCESS",
    "Stack": "appstream-storage"
  },
  {
    "Status": "SUCCESS",
    "Stack": "capability-storage"
  },
  {
    "Status": "SUCCESS",
    "Stack": "action-storage"
  },
  {
    "Status": "SUCCESS",
    "Stack": "domain-action-storage"
  },
  {
    "Status": "FAILED",
    "Stack": "auth0-clients"
  },
  {
    "Status": "FAILED",
    "Stack": "ecr-repository"
  },
  {
    "Status": "FAILED",
    "Stack": "ecr-replication"
  }
]

I think I need a Choice state but I cannot figure out if the language supports this.

If all I have are SUCCESS items, then the Choice goes to a All succeed state, and if I have at least one item with FAILED, then the Choice goes to a Partially succeed state. If I could have the failed items carried over into the Partially succeed would be nice, but not mandatory.

答案1

得分: 2

我认为您想要执行类似以下操作的操作。这里的想法是在JSON路径处理中使用筛选表达式来将数组分为两个列表,一个用于失败的元素,一个用于成功的元素。然后,您可以将其发送到并行状态,在那里您可以为成功的项目和失败的项目分别创建分支。

这个示例有点冗长,但可以让您将其放入您的帐户中并查看其工作原理。实际上,输出很可能来自一个任务,您可以使用ResultSelector来格式化输出为单独的项目,而不需要Pass状态。此外,在并行状态的分支中,您可以使用InputPathParameters来获取输入的正确部分,而不需要添加Pass状态(如果您不想要的话)。

在一个步进函数中筛选输入数组并重定向到不同的状态

{
  "Comment": "展示如何将数据发送到两个不同状态的示例",
  "StartAt": "生成输入",
  "States": {
    "生成输入": {
      "Type": "Pass",
      "Result": [
        {
          "Status": "SUCCESS",
          "Stack": "metadata"
        },
        {
          "Status": "FAILED",
          "Stack": "metadata-roles"
        },
        {
          "Status": "FAILED",
          "Stack": "master-storage"
        },
        {
          "Status": "SUCCESS",
          "Stack": "appstream-storage"
        },
        {
          "Status": "SUCCESS",
          "Stack": "capability-storage"
        },
        {
          "Status": "SUCCESS",
          "Stack": "action-storage"
        },
        {
          "Status": "SUCCESS",
          "Stack": "domain-action-storage"
        },
        {
          "Status": "FAILED",
          "Stack": "auth0-clients"
        },
        {
          "Status": "FAILED",
          "Stack": "ecr-repository"
        },
        {
          "Status": "FAILED",
          "Stack": "ecr-replication"
        }
      ],
      "Next": "拆分"
    },
    "拆分": {
      "Type": "Pass",
      "Parameters": {
        "success.$": "$.[?(@.Status == 'SUCCESS')]",
        "failed.$": "$.[?(@.Status == 'FAILED')]"
      },
      "Next": "并行"
    },
    "并行": {
      "Type": "Parallel",
      "End": true,
      "Branches": [
        {
          "StartAt": "获取成功",
          "States": {
            "获取成功": {
              "Type": "Pass",
              "OutputPath": "$.success",
              "Next": "处理成功"
            },
            "处理成功": {
              "Type": "Pass",
              "End": true
            }
          }
        },
        {
          "StartAt": "获取失败",
          "States": {
            "获取失败": {
              "Type": "Pass",
              "OutputPath": "$.failed",
              "Next": "处理失败"
            },
            "处理失败": {
              "Type": "Pass",
              "End": true
            }
          }
        }
      ]
    }
  }
}
英文:

I think you want to do something like the following. The idea here is to use a filter expression in JSON Path Processing to split the array into two lists, one for failed and one for success. You then send that into a Parallel state where you can have one branch for your successful items and one for your failed ones.

This example is a bit verbose but will allow you to drop it into your account and see how it works. I reality, the output would likely come from a Task and you could use the ResultSelector to format the output into separate items without needing a Pass state. As well, in the branches for the Parallel state, you could use InputPath or Parameters to grab the right part of the input without needing to add a Pass state if you didn't want to.

在一个步进函数中筛选输入数组并重定向到不同的状态

{
"Comment": "A example to show how to send data to two separate states",
"StartAt": "Generate Input",
"States": {
"Generate Input": {
"Type": "Pass",
"Result": [
{
"Status": "SUCCESS",
"Stack": "metadata"
},
{
"Status": "FAILED",
"Stack": "metadata-roles"
},
{
"Status": "FAILED",
"Stack": "master-storage"
},
{
"Status": "SUCCESS",
"Stack": "appstream-storage"
},
{
"Status": "SUCCESS",
"Stack": "capability-storage"
},
{
"Status": "SUCCESS",
"Stack": "action-storage"
},
{
"Status": "SUCCESS",
"Stack": "domain-action-storage"
},
{
"Status": "FAILED",
"Stack": "auth0-clients"
},
{
"Status": "FAILED",
"Stack": "ecr-repository"
},
{
"Status": "FAILED",
"Stack": "ecr-replication"
}
],
"Next": "Split"
},
"Split": {
"Type": "Pass",
"Parameters": {
"success.$": "$.[?(@.Status == 'SUCCESS')]",
"failed.$": "$.[?(@.Status == 'FAILED')]"
},
"Next": "Parallel"
},
"Parallel": {
"Type": "Parallel",
"End": true,
"Branches": [
{
"StartAt": "Get Success",
"States": {
"Get Success": {
"Type": "Pass",
"OutputPath": "$.success",
"Next": "Process Successful"
},
"Process Successful": {
"Type": "Pass",
"End": true
}
}
},
{
"StartAt": "Get Failed",
"States": {
"Get Failed": {
"Type": "Pass",
"OutputPath": "$.failed",
"Next": "Process Failed"
},
"Process Failed": {
"Type": "Pass",
"End": true
}
}
}
]
}
}
}

huangapple
  • 本文由 发表于 2023年2月23日 23:20:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546811.html
匿名

发表评论

匿名网友

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

确定