我正在使用带有C#驱动程序的dynamoDB,我有一个用户表.该表具有以下两个主键:
>主哈希键:UserId(数字)
>主范围键:创建(字符串)
然后我尝试在上下文中使用Load方法加载User,如下所示:
_dynamoDBClient.Context.Load<User>(12345);
然后我得到以下例外:
“exceptionMessage”: “Unable to convert range key value for property
Created”, “exceptionType”: “System.InvalidOperationException”
如果我加载特定的范围键,如:
_dynamoDBClient.Context.Load<User>(12345, "2015-01-01");
一切都好.
是否有办法只使用主哈希键加载一个键入的用户,即使该表有一个范围键?我不希望每次需要获取用户时都发送创建日期.或者我是否想过了解dynamoDB中范围键的概念?
解决方法:
Load方法用于从表中检索单个项目,因此您需要提供整个主键.
在幕后,Load方法实际上从本机AWS DynamoDB API调用GetItem操作,该API对需要提供的属性有以下要求:
For the primary key, you must provide all of the attributes. For
example, with a hash type primary key, you only need to provide the
hash attribute. For a hash-and-range type primary key, you must
provide both the hash attribute and the range attribute.
资源:
http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html
如果表的主键由Hash和Range Key组成,则必须提供两者以匹配单个项.
现在,当涉及到您的数据模型时,Range Key用于对通常一起检索的相关记录进行分组.在您的情况下,我假设有其他用户具有相同的UserId,但具有不同的创建日期.如果您不需要将用户组合在一起(并按创建日期排序),那么只有Hash Key就足够了.
这两个帖子可能会帮助您确定适用于不同场景的正确密钥类型:
在dynamodb表中使用Hash范围有什么用?
What is the use of a Hash range in a dynamodb table?
什么时候用什么PK型?
DynamoDB: When to use what PK type?
原文地址:https://codeday.me/bug/20190612/1222991.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。