搜索DynamoDB表上的数组项 [英] searching on array items on a DynamoDB table

查看:143
本文介绍了搜索DynamoDB表上的数组项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要了解如何搜索属于数组的DynamoDB的属性。

I need to understand how one can search attributes of a DynamoDB that is part of an array.

因此,在对表格进行非规范化处理时,请说一个拥有多个电子邮件地址的人。我会在人员表中创建一个数组来存储电子邮件地址。

So, in denormalising a table, say a person that has many email addresses. I would create an array into the person table to store email addresses.

现在,因为电子邮件地址不是排序键的一部分,如果我需要执行一个搜索电子邮件地址以查找人员记录。我需要为电子邮件属性编制索引。

Now, as the email address is not part of the sort key, and if I need to perform a search on an email address to find the person record. I need to index the email attribute.


  1. 我可以在电子邮件地址上创建一个索引,这与人员记录是1-many关系它根据我在DynamoDB中的理解存储为数组。

  2. 这个二级索引是全局的还是本地的?假设我有数十亿人的记录?

  1. Can I create an index on the email address, which is 1-many relationship with a person record and it's stored as an array as I understand it in DynamoDB.
  2. Would this secondary index be global or local? Assuming I have billions of person records?

  1. 如果我可以将其创建为LSI或GSI,请解释每个的优点/缺点。


非常感谢!

推荐答案

Stu的回答里面有一些很好的信息,而且他是对的,你不能使用一个数组它自己作为一个关键。

Stu's answer has some great information in it and he is right, you can't use an Array it's self as a key.


你可以有时 do将多个变量(或数组)连接成一个带有已知分隔符的单个字符串(例如,可能是'_'),然后将该字符串用作排序键。

What you CAN sometimes do is concatenate several variables (or an Array) into a single string with a known seperator (maybe '_' for example), and then use that string as a Sort Key.

I使用此概念创建一个由多个ISO 8061日期对象组成的复合排序键(DyanmoDB在String类型属性中将日期存储为ISO 8061)。我还使用了几个不是日期但属于固定字符长度的整数的属性。

I used this concept to create a composite Sort Key that consisted of multiple ISO 8061 date objects (DyanmoDB stores dates as ISO 8061 in String type attributes). I also used several attributes that were not dates but were integers with a fixed character length.

通过使用BETWEEN比较,我可以单独查询连接到排序键的每个变量,或构建一个与所有变量匹配的复杂查询一个小组。

By using the BETWEEN comparison I am able to individually query each of the variables that are concatenated into the Sort Key, or construct a complex query that matches against all of them as a group.

换句话说,数据对象可以使用这样的排序键:
email @ gmail.com_email @ msn .com_email @ someotherplace.com

In other words a data object could use a Sort Key like this: email@gmail.com_email@msn.com_email@someotherplace.com

然后你可以用这样的东西查询(假设你知道分区键是什么):

Then you could query that (assuming you knew what the partition key is) with something like this:

SELECT * FROM Users
WHERE User ='Bob'和电子邮件LIKE'%email@msn.com%'

我认为你问的真正问题是我的排序键和分区键应该是什么?这取决于你想要做出哪些查询以及使用每种类型的查询的频率。

I think the real question you are asking is what should my sort keys and partition keys be? That will depend on exactly which queries you want to make and how frequently each type of query is used.

我发现如果我认为我在DynamoDB上取得了更大的成功关于我想先做的查询,然后从那里开始。

I have found that I have way more success with DynamoDB if I think about the queries I want to make first, and then go from there.

这里的问题是你仍然需要知道你的辅助数据结构的分区密钥。 GSI / LSI帮助您避免创建额外的DynamoDB表,仅用于改善数据访问。

The issue here is that you still need to 'know' the Partition Key for your secondary data structure. GSI / LSI help you avoid needing to create additional DynamoDB tables for the sole purpose of improving data access.

来自亚马逊:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SecondaryIndexes.html

对我而言,这听起来更像问题是选择密钥。

To me it sounds more like the issue is selecting the Keys.

LSI(本地二级索引)
如果(对于您的查询案例)您不知道开始的分区键(因为您似乎没有),那么本地二级索引将无济于事 - 因为它有相同的分区密钥作为基表。

LSI (Local Secondary Index) If (for your Query case) you don't know the Partition Key to begin with (as it seems you don't) then a Local Secondary Index won't help — since it has the SAME Partition Key as the base table.

GSI(全球二级索引)
全球二级索引可以帮助你可以拥有一个不同的分区键和排序键(可能是你可以'知道'这个查询的分区键)。

GSI (Global Secondary Index) A Global Secondary Index could help in that you can have a DIFFERENT Partition Key and Sort Key (presumably a partition key that you could 'know' for this query).

所以你可以使用Ema il属性(可能是复合的)作为GSI上的排序键,然后是服务名称或注册阶段,作为分区键。这将让您了解用户根据他们的进度或他们注册的服务(例如)所在的分区。

So you could use the Email attribute (perhaps composite) as the Sort Key on your GSI and then something like a service name, or sign-up stage, as your Partition Key. This would let you 'know' what partition that user would be in based on their progress or the service they signed up from (for example).

GSI / LSI仍然需要使用他们的密钥生成唯一值,所以请记住这一点!

这篇关于搜索DynamoDB表上的数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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