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

查看:90
本文介绍了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天全站免登陆