在没有哈希键或扫描的情况下在Dynamo DB中查询 [英] Query in Dynamo DB without hashkey or scan

查看:62
本文介绍了在没有哈希键或扫描的情况下在Dynamo DB中查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在没有HashKey的Dynamo DB上进行查询。我尝试使用扫描,但是它很昂贵,因此需要寻找其他替代方法。

I am looking to query on Dynamo DB without HashKey. I have tried using the scan but it is expensive so looking for some other alternatives.

推荐答案

我应该首先说一下查询一个无法知道哈希键的DynamoDB表无法完成。

I should start by saying that querying a DynamoDB table without knowing the hash key can't be done. And it makes sense.

现在,您要使用的哈希键是否是表的主键,取决于您。

Now, whether the hash key you want to use is the primary key of the table or not, is up to you.

例如,假设您有下表:

╔══════════════════════╦════════════════════════╦════════════════╗
║ course_id (Hash Key) ║      course_name       ║    teacher     ║
╠══════════════════════╬════════════════════════╬════════════════╣
║ 324234               ║ Node.js for Dummies    ║ Ryan Dahl      ║
║ 213323               ║ How to train your cat  ║ Jackson Galaxy ║
║ 324090               ║ Cat Logic              ║ Jackson Galaxy ║
║ 763298               ║ Diving into .NET       ║ Eric Lippert   ║
╚══════════════════════╩════════════════════════╩════════════════╝

表的主键和哈希键为 course_id ,这很好。提供唯一的哈希键可以将您的表拆分为多个分区。

The table's primary key and hash key is course_id, which is fine. Providing unique hash keys allows your table to be split into multiple partitions.

但是,如果我们想获得 Jackson Galaxy正在教授的所有课程,该怎么办

But, what if we want to get All the courses that Jackson Galaxy is teaching?

我们不知道这些课程的 course_id ,这就是我们想要得到的。因此,我们发现自己时并不知道这些项的哈希键值。

We don't know the course_id of those courses, that's what we want to get. So we find ourselves without knowing the items' hash key values.

这就是 GSI 发挥作用的地方。 全局二级索引可让您为表定义其他哈希键。 请注意,它不会更改主哈希键- course_id 仍将是表的哈希键。

That's where GSI come into play. Global Secondary Indexes let you define a different hash key for your table. Note that it will not change the primary hash key - course_id will still be the table's hash key.

GSI仅为您提供一个附加的哈希键,以便您能够进行更复杂的查询。

GSI only provides an additional hash key for you to be able to make more complex queries.

比方说,我们添加了一个名为 teacher_index ,我们说 teacher 是我们的哈希键,而 course_id 将是我们的范围键(我们需要指定一个范围键,因为仅 teacher 作为哈希键会生成重复的条目)。

Let's say we add a GSI named teacher_index, and we say that teacher will be our hash key, and course_id will be our range key (We need to specify a range key, because teacher alone as a hash key will generate duplicate entries).

现在,我们可以查询 teacher_index 并将 Jackson Galaxy 作为哈希键值传递。结果将是 213323-如何训练猫咪 324090-Cat Logic

Now we can query our teacher_index and pass in Jackson Galaxy as a hash key value. The results would be 213323 - How to train your cat and 324090 - Cat Logic.

这篇关于在没有哈希键或扫描的情况下在Dynamo DB中查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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