AWS DynamoDB - 随机选择记录/项目? [英] AWS DynamoDB - Pick a record/item randomly?

查看:27
本文介绍了AWS DynamoDB - 随机选择记录/项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法如何从 DynamoDB 表中随机选择项目/记录?我不相信 API 中对此有任何规定.

Any ideas how to pick an item/record randomly from a DynamoDB table? I don't believe there are any provisions for this in the API.

我考虑过维护一个 NumericId|MyOtherKey ("NumericIdTable") 表,然后生成一个介于 0 和我拥有的记录总数之间的随机数,然后从 NumericIdTable 获取该项目,但它不会长期工作-运行.

I thought about maintaining a table of NumericId|MyOtherKey ("NumericIdTable") and then generating a random number between 0 and the total number of records I have, then getting that item from NumericIdTable but it's not going to work in the long-run.

欢迎提出想法/想法.

推荐答案

我想出的一种方法是从 DynamoDB 表中随机选择一个项目:

One approach I came up with to pick a random item from a DynamoDB Table:

  1. 为表中所有可能的 RangeKey 生成一个随机 RangeKey
  2. 使用此 RangeKey 和 RangeKeyCondition GreaterThan 以及限制为 1 查询表

例如,如果您使用 UUID 作为 RangeKey 的标识符,您可以获得如下所示的随机项目

For example if you use a UUID as Identifier for your RangeKey you could get your random Item like the following

RandomRangeKey = new UUID
RandomItem = Query( "HashKeyValue": "KeyOfRandomItems",
                    "RangeKeyCondition": { "AttributeValueList":
                                "RandomRangeKey",
                                "ComparisonOperator":"GT"}, 
                    "Limit": 1 )

这样你会得到一个随机的项目并且只消耗 1 个读取容量.

This way you get a random Item and only consume 1 read capacity.

通过生成比表中使用的最小 UUID 更小的 UUID,有可能错过对随机变量的第一次查询.这个机会随着表的扩大而缩小,您可以使用 SmallerThan 比较对同一随机键轻松发送另一个请求,从而确保随机项目的命中.

There is a chance to miss the first query for a random variable by generating a smaller UUID than the smallest one used in the table. This chance scales down with the table scaling up and you can easily send another request using the SmallerThan Comparison on the same random key, which then ensures a hit for a random item.

如果您的 Tabledesign 不允许可随机化的 RangeKey,您可以按照您的方法创建一个单独的 RandomItem 表并将 ID 存储在可随机化的 RangeKey 下.一个可能的表结构是

If your Tabledesign doesn't allow randomizable RangeKeys you could follow your approach and create a separate RandomItem table and store the ID under a randomizable RangeKey. A possible table structure for this would be

*RandomItemTable
   TableName - HashKey
   UUID - Rangekey
   ItemId

请记住,对于这种方法,您需要管理原始表和随机表之间的冗余.

Keep in mind, for this approach you need to manage the redundancy between the original table and the randomization table.

这篇关于AWS DynamoDB - 随机选择记录/项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆