DynamoDB 数据建模
DynamoDB data modeling
有一个代表用户唯一性的 java BitSet,我想存储到 DynamoDB 中以便使用像 "give me all BitSets from date X to date Y with a concrete key".
这样的查询
我的第一个方法是使用一个主键来表示我真正想要计算的内容,例如一个动作:"users-who-pay"。然后范围键是日期,最后我将值转换为二进制属性。
但这可能不是一个好方法,因为我会有一些键和很多日期,所以我想知道是否有人向我推荐另一种方法。
使用 HashMap<String userId, Integer>
怎么样?这样做的好处是:
1- 地图的大小将根据需要变大。
2- 你可以数的比 1 多。使用 BitSet 你只能数 0 或 1。
3- 地图由 DynamoDb 原生支持。
编辑:或者如果您不需要计算超过 1,请使用 HashSet。如果 userId 在 HashSet 中,则意味着您命中了。如果不在HashSet中,则没有命中。
DynamoDB 文档中的一个部分处理类似的用例。参见 Take Advantage of Sparse Indexes
Take Advantage of Sparse Indexes
For any item in a table, DynamoDB will only write a corresponding entry to a global secondary index if the index key value is present in the item. For global secondary indexes, this is the index hash key and its range key (if present). If the index key value(s) do not appear in every table item, the index is said to be sparse.
You can use a sparse global secondary index to efficiently locate table items that have an uncommon attribute. To do this, you take advantage of the fact that table items that do not contain global secondary index attribute(s) are not indexed at all. For example, in the GameScores table, certain players might have earned a particular achievement for a game - such as "Champ" - but most players have not. Rather than scanning the entire GameScores table for Champs, you could create a global secondary index with a hash key of Champ and a range key of UserId. This would make it easy to find all the Champs by querying the index instead of scanning the table.
这样的查询会非常高效,因为索引中的项目数将明显少于 table 中的项目数。此外,投射到索引中的 table 属性越少,从索引中消耗的读取容量单位就越少。
该示例听起来与您的“付费用户”用例非常相似 - 唯一的区别是(将“冠军”替换为“付费用户”)。然而,它谈论的是很少有用户是冠军的情况(这就是为什么可以将“冠军”作为散列键 - 在此处阅读更多关于好的散列键的信息 - http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GuidelinesForTables.html)。这可以通过说你有(比方说)100 个 champs 的散列键来补救——champ00、champ01、...、champ99。在将条目写入 DynamoDB 时,可以随机选择其中一个值。
有一个代表用户唯一性的 java BitSet,我想存储到 DynamoDB 中以便使用像 "give me all BitSets from date X to date Y with a concrete key".
这样的查询我的第一个方法是使用一个主键来表示我真正想要计算的内容,例如一个动作:"users-who-pay"。然后范围键是日期,最后我将值转换为二进制属性。
但这可能不是一个好方法,因为我会有一些键和很多日期,所以我想知道是否有人向我推荐另一种方法。
使用 HashMap<String userId, Integer>
怎么样?这样做的好处是:
1- 地图的大小将根据需要变大。
2- 你可以数的比 1 多。使用 BitSet 你只能数 0 或 1。
3- 地图由 DynamoDb 原生支持。
编辑:或者如果您不需要计算超过 1,请使用 HashSet。如果 userId 在 HashSet 中,则意味着您命中了。如果不在HashSet中,则没有命中。
DynamoDB 文档中的一个部分处理类似的用例。参见 Take Advantage of Sparse Indexes
Take Advantage of Sparse Indexes
For any item in a table, DynamoDB will only write a corresponding entry to a global secondary index if the index key value is present in the item. For global secondary indexes, this is the index hash key and its range key (if present). If the index key value(s) do not appear in every table item, the index is said to be sparse.
You can use a sparse global secondary index to efficiently locate table items that have an uncommon attribute. To do this, you take advantage of the fact that table items that do not contain global secondary index attribute(s) are not indexed at all. For example, in the GameScores table, certain players might have earned a particular achievement for a game - such as "Champ" - but most players have not. Rather than scanning the entire GameScores table for Champs, you could create a global secondary index with a hash key of Champ and a range key of UserId. This would make it easy to find all the Champs by querying the index instead of scanning the table.
这样的查询会非常高效,因为索引中的项目数将明显少于 table 中的项目数。此外,投射到索引中的 table 属性越少,从索引中消耗的读取容量单位就越少。
该示例听起来与您的“付费用户”用例非常相似 - 唯一的区别是(将“冠军”替换为“付费用户”)。然而,它谈论的是很少有用户是冠军的情况(这就是为什么可以将“冠军”作为散列键 - 在此处阅读更多关于好的散列键的信息 - http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GuidelinesForTables.html)。这可以通过说你有(比方说)100 个 champs 的散列键来补救——champ00、champ01、...、champ99。在将条目写入 DynamoDB 时,可以随机选择其中一个值。