Range key未知时如何查询DynamoDB
How to Query DynamoDB when Range key is unknown
我有一个具有以下 KeySchema 的 table:
KeySchema: [
{AttributeName: USER_ID,KeyType: HASH},
{AttributeName: COMPLETED,KeyType: RANGE}],
用户 ID 只是一个整数,但 COMPLETED
是记录创建时间的时间戳。
我正在尝试使用此代码检索记录:
Item item = tbl.getItem("USER_ID", "4216634082");
但是我得到这个错误:
AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException...
由于范围键是时间戳,在找到记录之前我不知道它是什么。我该如何实现?
您正在调用 getItem
,这是通过键查找项目 - 如果键架构定义了两者,则这需要散列和范围组件。
如果您想获取所有具有特定哈希值的项目(按范围键排序),您需要使用查询。例如:
Table table = dynamoDB.getTable("YourTable");
QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("USER_ID = :user_id")
.withValueMap(new ValueMap()
.withString(":user_id", "4216634082"))
ItemCollection<QueryOutcome> items = table.query(spec);
有关 Java SDK 文档的详细信息,请参阅 http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html for concepts and http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html。
(从文档链接中提取的示例)
我有一个具有以下 KeySchema 的 table:
KeySchema: [
{AttributeName: USER_ID,KeyType: HASH},
{AttributeName: COMPLETED,KeyType: RANGE}],
用户 ID 只是一个整数,但 COMPLETED
是记录创建时间的时间戳。
我正在尝试使用此代码检索记录:
Item item = tbl.getItem("USER_ID", "4216634082");
但是我得到这个错误:
AmazonDynamoDBException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException...
由于范围键是时间戳,在找到记录之前我不知道它是什么。我该如何实现?
您正在调用 getItem
,这是通过键查找项目 - 如果键架构定义了两者,则这需要散列和范围组件。
如果您想获取所有具有特定哈希值的项目(按范围键排序),您需要使用查询。例如:
Table table = dynamoDB.getTable("YourTable");
QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("USER_ID = :user_id")
.withValueMap(new ValueMap()
.withString(":user_id", "4216634082"))
ItemCollection<QueryOutcome> items = table.query(spec);
有关 Java SDK 文档的详细信息,请参阅 http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html for concepts and http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html。
(从文档链接中提取的示例)