DynamoDB - Global Secondary Index on set items

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

I have a dynamo table with the following attributes :

  • id (Number - primary key )
  • title (String)
  • created_at (Number - long)
  • tags (StringSet - contains a set of tags say android, ios, etc.,)

I want to be able to query by tags - get me all the items tagged android. How can I do that in DynamoDB? It appears that global secondary index can be built only on ScalarDataTypes (which is Number and String) and not on items inside a set.

If the approach I am taking is wrong, an alternative way for doing it either by creating different tables or changing the attributes is also fine.

回答1:

The key schema for the index. Every attribute in the index key schema must be a top-level attribute of type String, Number, or Binary. Nested attributes and multi-valued sets are not allowed. Other requirements for the key schema depend on the type of index: For a global secondary index, the hash attribute can be any scalar table attribute. A range attribute is optional, and it too can be any scalar table attribute. For a local secondary index, the hash attribute must be the same as the table's hash attribute, and the range attribute must be a non-key table attribute.

  • Amazon recommends creating a separate one-to-many table for these kind of problems. More info here : Use one to many tables


回答2:

You will need to create a separate table for this query. If you are interested in fetching all items based on a tag then I suggest keeping a table with a primary key:
hash: tag
range: id

This way you can use a very simple Query to fetch all items by tag.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!