Ada*_*dam 9 amazon database-design amazon-dynamodb
DynamoDB新手.
我正在使用主键'UserID',复合键'DateTime'创建一个表,然后我将以下值作为值(注意:我不需要查询以下数据中的任何细节 - 只需写入并读取它) :
UserID1
UserID2
Message
DateTime
Run Code Online (Sandbox Code Playgroud)
问题:
所以你的选择是:
Hash Key | Range Key | Attributes
----------------------------------
user id | utc time | json data
----------------------------------
user123 | 1357306017 | {UserID1:0, UserID2:0, Message:"", DateTime:0}
Run Code Online (Sandbox Code Playgroud)
要么
Hash Key | Range Key | Attributes
--------------------------------------------------------------
user id | utc time | UserID1 | UserID2 | Message | DateTime
--------------------------------------------------------------
user123 | 1357306017 | 0 | 0 | "" | 0
Run Code Online (Sandbox Code Playgroud)
两者都是可行的选项,选择取决于您希望如何读取数据,如果您有每个项目的属性,那么您可以单独请求这些属性.
我们倾向于使用基于我们的使用模式的混合方法.我们需要单独访问的元素具有自己的属性.我们只想访问的元素以及其他元素的集合都被分配了一个属性,然后存储为单个blob的JSON字符串或base64编码数据.
实际上,对于第二部分,您是对的,您不需要再次将用户ID和日期时间存储为属性的一部分,因为它们是哈希和范围键,它们在您发出请求时返回.
您可以将JSON blob中的条目存储为单独的AttributeValues.在DynamoDB引入JSON文档支持之前,您的选项将仅限于单独的属性,或者一个"String"属性,您可以在其中存储这些属性的JSON表示.现在,亚马逊为DynamoDB引入了JSON文档支持,您可以将这种详细的属性映射直接存储在项目中.使用新的Java Document SDK for DynamoDB,添加JSON值使用Item.withJSON()方法,如下所示:
DynamoDB dynamodb = new DynamoDB(client);
Table messagesTable = dynamodb.getTable("MESSAGES");
// create the item
Item item = new Item().withString("UserID", "user123").withString("DateTime", "1357306017")
.withJSON("Details", "{ \"UserID1\": 0, \"UserID2\": 0, \"Message\": \"my message\", \"DateTime\": 0}");
// put the item
messagesTable.putItem(item);
// get the item
Item itemGet = messagesTable.getItem(new KeyAttribute("UserID", "user123"), new KeyAttribute("DateTime", "1357306017"));
Run Code Online (Sandbox Code Playgroud)我同意Pooky的说法,没有必要在详细地图中复制Hash + Range键.您需要使用这两个来获取项目.
我假设“单独的项目”是指“单独的属性”,在这种情况下它并不重要。我可能会将它们存储为单独的属性,因为可以检索属性的子集(尽管您说您现在不需要此功能)。将来,如果您想查看用户发送了多少消息,但又不想等待慢速网络返回许多 KB 的消息,那么拥有单独的属性将会很有用。
是的。
| 归档时间: |
|
| 查看次数: |
13103 次 |
| 最近记录: |