如何从AWS Java SDK的ExecuteStatementRequest响应中获取属性值?
从DynamoDB ExecuteStatementResponse中提取属性值
你遇到的问题是没有用对SDK提供的类型转换方法,ExecuteStatementResponse里的Items实际是List<Map<String, AttributeValue>>类型,不能直接用getValueForField转成Collections.class。下面是具体的提取步骤:
正确的提取代码
替换你原来的打印代码,用SDK提供的类型安全方法来取值:
// 获取返回的所有条目(UPDATE操作这里只会返回1条) List<Map<String, AttributeValue>> items = response2.items(); if (!items.isEmpty()) { // 取出第一条更新后的条目 Map<String, AttributeValue> updatedItem = items.get(0); // 提取thresholdValue属性,转成Double类型 AttributeValue thresholdValueAttr = updatedItem.get("thresholdValue"); Double thresholdValue = Double.valueOf(thresholdValueAttr.n()); // 提取assignmentId属性(如果需要) AttributeValue assignmentIdAttr = updatedItem.get("assignmentId"); String assignmentId = assignmentIdAttr.s(); // 打印结果 System.out.println("更新后的thresholdValue: " + thresholdValue); System.out.println("对应的assignmentId: " + assignmentId); }
关键说明
- 直接用
response2.items()获取返回条目列表,这是AWS SDK v2提供的类型安全方法,比getValueForField更可靠。 - 每个条目是一个
Map<String, AttributeValue>,key是属性名,value是DynamoDB的属性值对象。 - 根据属性的存储类型调用对应的方法:
- 字符串类型用
attr.s() - 数字类型用
attr.n()(返回字符串,需要自行转成对应数值类型) - 布尔类型用
attr.bool() - 列表/集合类型用
attr.l()或attr.ss()等
- 字符串类型用
内容的提问来源于stack exchange,提问作者João Pedro Schmitt
相关产品推荐
相关产品推荐

