在没有 hashkey 或扫描的 Dynamo DB 中查询 [英] Query in Dynamo DB without hashkey or scan

查看:25
本文介绍了在没有 hashkey 或扫描的 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.

但是,如果我们想获得杰克逊银河教授的所有课程?

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 的 GSI,我们说 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.

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

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