You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在AWS Step Functions中按索引访问DynamoDB查询结果Items数组项

在AWS Step Functions的Choice状态中访问DynamoDB Query结果数组元素

你在AWS Step Functions中配置了DynamoDB Query任务,返回结果嵌套在processed_files路径下,需要在Choice状态中基于数组内的元素做流程判断,但常规索引方式无法访问。

你的DynamoDB Query任务配置

{
  "Type": "Task",
  "Resource": "arn:aws:states:::aws-sdk:dynamodb:query",
  "Next": "If nothing returned by query Or Study not yet Zipped",
  "Parameters": {
    "TableName": "TEST-StudyProcessingTable",
    "ScanIndexForward": false,
    "Limit": 1,
    "KeyConditionExpression": "OrderID = :OrderID",
    "FilterExpression": "StudyID = :StudyID",
    "ExpressionAttributeValues": {
      ":OrderID": {
        "S.$": "$.body.order_id"
      },
      ":StudyID": {
        "S.$": "$.body.study_id"
      }
    }
  },
  "ResultPath": "$.processed_files"
}

Query返回结果结构

{
  "body": {
    "order_id": "1001",
    "study_id": "1"
  },
  "processed_files": {
    "Count": 1,
    "Items": [
      {
        "Status": {
          "S": "unzipped"
        },
        "StudyID": {
          "S": "1"
        },
        "ZipFileS3Key": {
          "S": "path/to/the/file"
        },
        "UploadSet": {
          "S": "4"
        },
        "OrderID": {
          "S": "1001"
        },
        "UploadSet#StudyID": {
          "S": "4#1"
        }
      }
    ],
    "LastEvaluatedKey": {
      "OrderID": {
        "S": "1001"
      },
      "UploadSet#StudyID": {
        "S": "4#1"
      }
    },
    "ScannedCount": 1
  }
}

解决方案

核心要点

Step Functions使用的是亚马逊风格的JsonPath,和常规JsonPath存在细微差异,同时DynamoDB返回的属性自带类型标识(如S代表字符串),需注意:

  • 数组索引使用[索引数字]格式,不能用['索引字符串']
  • 必须深入到对应类型字段(如字符串类型取S)才能获取实际属性值

Choice状态配置示例

以下是实现两种关键判断的Choice状态配置:

  • 判断Query未返回任何结果(Count=0)
  • 判断返回的第一个Item的Status为unzipped
{
  "Type": "Choice",
  "Choices": [
    {
      "Variable": "$.processed_files.Count",
      "NumericEquals": 0,
      "Next": "Handle No Results"
    },
    {
      "Variable": "$.processed_files.Items[0].Status.S",
      "StringEquals": "unzipped",
      "Next": "Handle Unzipped Study"
    }
  ],
  "Default": "Handle Zipped Study"
}

注意事项

  • 优先判断Count是否大于0,避免数组为空时访问Items[0]引发错误
  • 如果Query可能返回多个Item,可按需调整索引(如Items[1])或结合其他条件处理
  • 不同数据类型需对应访问各自的类型字段(如数字类型取N、布尔类型取BOOL)

内容的提问来源于stack exchange,提问作者Avramo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 23:39:16