本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在 Step Functions ResultPath 中使用指定状态输出
管理状态和转换数据
本页指的是JSONPath。Step Functions 最近添加了变量JSONata,用于管理状态和转换数据。
了解如何使用变量传递数据和使用转换数据JSONata。
状态的输出可以是其输入的副本、其生成的结果(例如,Task
状态的 Lambda 函数的输出)或其输入和结果的组合。使用 ResultPath
可控制传递到状态输出的上述两种内容的组合。
以下状态类型可以生成结果且包含 ResultPath:
使用 ResultPath
可将任务结果与任务输入组合,或可选择二者之一。提供给 ResultPath
的路径控制将传递到输出的信息。
注意
ResultPath
仅限于使用引用路径,因为引用路径限制了范围,因此路径只能标识中的单个节点JSON。请参阅 Amazon States Language 中的 引用路径。
用于 ResultPath 用任务结果替换输入
如果未指定ResultPath
,则默认行为与相同"ResultPath": "$"
。该状态将用任务的结果替换整个状态输入。
# State Input
{
"comment": "This is a test",
"details": "Default example",
"who" : "Step Functions"
}
# Path
"ResultPath": "$"
# Task result
"Hello, Step Functions!"
# State Output
"Hello, Step Functions!"
注意
ResultPath
用于包含结果内容与输入,然后再将其传递到输出。但是,如果ResultPath
未指定,则默认操作是替换整个输入。
丢弃结果并保留原始输入
如果设置ResultPath
为null
,则状态会将原始输入传递给输出。状态的输入有效载荷将直接复制到输出中,而不考虑任务结果。
# State Input
{
"comment": "This is a test",
"details": "Default example",
"who" : "Step Functions"
}
# Path
"ResultPath": null
# Task result
"Hello, Step Functions!"
# State Output
{
"comment": "This is a test",
"details": "Default example",
"who" : "Step Functions"
}
用于 ResultPath 将结果包含在输入中
如果您为指定路径 ResultPath,则状态输出将合并状态输入和任务结果:
# State Input
{
"comment": "This is a test",
"details": "Default example",
"who" : "Step Functions"
}
# Path
"ResultPath": "$.taskresult"
# Task result
"Hello, Step Functions!"
# State Output
{
"comment": "This is a test",
"details": "Default example",
"who" : "Step Functions",
"taskresult" : "Hello, Step Functions!"
}
还可以将结果插入输入的子节点。将 ResultPath
设置为以下内容。
"ResultPath": "$.strings.lambdaresult"
给定以下输入:
{ "comment": "An input comment.", "strings": { "string1": "foo", "string2": "bar", "string3": "baz" }, "who": "AWS Step Functions" }
任务结果将作为节点的子strings
节点插入到输入中。
{ "comment": "An input comment.", "strings": { "string1": "foo", "string2": "bar", "string3": "baz", "lambdaresult": "Hello, Step Functions!" }, "who": "AWS Step Functions" }
状态输出现在包括原始输入JSON,结果作为子节点。
ResultPath 用于用结果更新输入中的节点
如果您为指定现有节点 ResultPath,则任务结果将替换该现有节点:
# State Input
{
"comment": "This is a test",
"details": "Default example",
"who" : "Step Functions"
}
# Path
"ResultPath": "$.comment"
# Task result
"Hello, Step Functions!"
# State Output
{
"comment": "Hello, Step Functions!",
"details": "Default example",
"who" : "Step Functions"
}
ResultPath 用于在 a 中同时包含错误和输入 Catch
在某些情况下,您可能希望保留原始输入与错误。在 Catch
中使用 ResultPath
可包含错误与原始输入,而不是替换它。
"Catch": [{ "ErrorEquals": ["States.ALL"], "Next": "NextTask", "ResultPath": "$.error" }]
如果上一 Catch
语句捕获错误,则它将在状态输入的 error
节点中包含结果。例如,对于以下输入:
{"foo": "bar"}
捕获错误时,状态输出为以下内容。
{ "foo": "bar", "error": { "Error": "
Error here
" } }
有关错误处理的更多信息,请参阅以下内容: