并行状态在步进功能中合并输出

时间:2019-01-09 08:20:49

标签: amazon-web-services aws-step-functions

是否可能具有以下类型的阶跃函数图,即从两个并行状态输出,一个组合状态:

enter image description here

如果是,此json的外观如何?如果没有,为什么?

4 个答案:

答案 0 :(得分:3)

并行任务始终输出一个数组(每个分支包含一个条目)。

您可以告诉AWS步骤函数在并行状态定义中使用"ResultPath": "$.ParallelOut"将输出附加到原始输入的新(或现有)属性中,但这似乎并不是您想要实现的。 / p>

合并并行任务的输出,可以利用"Type": "Pass"状态来定义要应用于JSON文档的转换。

例如,在下面的状态机中,我正在转换JSON数组...

[
  {
    "One": 1,
    "Two": 2
  },
  {
    "Foo": "Bar",
    "Hello": "World"
  }
]

...进入一些属性

{
  "Hello": "World",
  "One": 1,
  "Foo": "Bar",
  "Two": 2
}

Transform an array into properties with AWS Step Functions

{
    "Comment": "How to convert an array into properties",
    "StartAt": "warm-up",
    "States": {
      "warm-up": {
        "Type": "Parallel",
        "Next": "array-to-properties",
        "Branches": [
          {
            "StartAt": "numbers",
            "States": {
              "numbers": {
                "Type": "Pass",
                "Result": {
                    "One": 1,
                    "Two" : 2
                },
                "End": true
              }
            }
          },
          {
            "StartAt": "words",
            "States": {
              "words": {
                "Type": "Pass",
                "Result": {
                    "Foo": "Bar",
                    "Hello": "World"
                },
                "End": true
              }
            }
          }
        ]
      },
      "array-to-properties": {
        "Type": "Pass",
        "Parameters": {
          "One.$": "$[0].One",
          "Two.$": "$[0].Two",
          "Foo.$": "$[1].Foo",
          "Hello.$": "$[1].Hello"
        },
        "End": true
      }
    }
}

答案 1 :(得分:0)

您的图表在技术上是错误的,因为没有状态可以为其Next任务设置多个状态。您不能通过提供多个状态名称来以StartAt的形式启动状态机。另外,即使有可能我也看不到为什么要运行两个并行状态而不是一个并行状态,而将所有子状态拆分为两个。

答案 2 :(得分:0)

有可能与下面的相反图

enter image description here

并行状态应如下所示

"MyParallelState": {
  "Type": "Parallel",
  "InputPath": "$",
  "OutputPath": "$",
  "ResultPath": "$.ParallelResultPath",
  "Next": "SetCartCompleteStatusState",
  "Branches": [
    {
      "StartAt": "UpdateMonthlyUsageState",
      "States": {
        "UpdateMonthlyUsageState": {
          "Type": "Task",
          "InputPath": "$",
          "OutputPath": "$",
          "ResultPath": "$.UpdateMonthlyUsageResultPath",
          "Resource": "LambdaARN",
          "End": true
        }
      }
    },
    {
      "StartAt": "QueueTaxInvoiceState",
      "States": {
        "QueueTaxInvoiceState": {
          "Type": "Task",
          "InputPath": "$",
          "OutputPath": "$",
          "ResultPath": "$.QueueTaxInvoiceResultPath",
          "Resource": "LambdaARN",
          "End": true
        }
      }
    }

MyParallelState的输出将从Parallel state中的每个状态以数组形式填充。它们被填充在ParallelResultPath对象中,并将被传递到Next状态

{
  "ParallelResultPath": [
    {
      "UpdateMonthlyUsageResultPath": Some Output
    },
    {
      "QueueTaxInvoiceResultPath": Some Output
    }
  ]
}

答案 3 :(得分:0)

我们可以使用ResultSelector和Result Path将结果合并为一个对象

我们有一个类似的并行状态:

{
  "StartAt": "ParallelBranch",
  "States": {
    "ParallelBranch": {
      "Type": "Parallel",
      "ResultPath": "$",
      "InputPath": "$",
      "OutputPath": "$",
      "ResultSelector": {
        "UsersResult.$": "$[1].UsersUpload",
        "CustomersResult.$": "$[0].customersDataUpload"
      },
      "Branches": [
        {
          "StartAt": "customersDataUpload",
          "States": {
            "customersDataUpload": {
              "Type": "Pass",
              "ResultPath": "$.customersDataUpload.Output",
              "Result": {
                "CompletionStatus": "success",
                "CompletionDetails": null
              },
              "Next": "Wait2"
            },
            "Wait2": {
              "Comment": "A Wait state delays the state machine from continuing for a specified time.",
              "Type": "Wait",
              "Seconds": 2,
              "End": true
            }
          }
        },
        {
          "StartAt": "UsersUpload",
          "States": {
            "UsersUpload": {
              "Type": "Pass",
              "Result": {
                "CompletionStatus": "success",
                "CompletionDetails": null
              },
              "ResultPath": "$.UsersUpload.Output",
              "Next": "Wait1"
            },
            "Wait1": {
              "Comment": "A Wait state delays the state machine from continuing for a specified time.",
              "Type": "Wait",
              "Seconds": 1,
              "End": true
            }
          }
        }
      ],
      "End": true
    }
  },
  "TimeoutSeconds": 129600,
  "Version": "1.0"
}

enter image description here

输出将是:

{
  "UsersResult": {
    "Output": {
      "CompletionStatus": "success",
      "CompletionDetails": null
    }
  },
  "CustomersResult": {
    "Output": {
      "CompletionStatus": "success",
      "CompletionDetails": null
    }
  }
}