本文介绍了使用无服务器框架为dynamodb设置复合排序键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对dynamodb和无服务器是陌生的,并且我已经读过有关复合排序键.我认为它们可以解决我的问题,但是我不确定如何实现它们.在我的情况下,我有一个带有 Post 实体的表,该表具有以下字段:

I am new to dynamodb and serverless, and I have read about composite sort keys. I think they could be a solution to my problem, but I am not completely sure how to implement them. In my case I have a table with a Post entity that has the following fields, it looks like this:

post_id <string> | user_id <string> | tags <string[]> | public <boolean>| other post data attributes...

我需要做的查询是:

  1. 获取所有标记为公开的帖子
  2. 获取按标签过滤的帖子
  3. 通过用户过滤所有公开和非公开的帖子
  4. 获取单个帖子

我只能将public属性设置为标记有public的实体.如何使用无服务框架定义复合排序键.

I could set the public attribute only to the entities that are marked with public.How does one define composite sort keys with servless framework.

例如,

sort key: tag#public

这是我之前设置的.

PostsDynamoDBTable:
      Type: 'AWS::DynamoDB::Table'
      Properties:
        AttributeDefinitions:
          - AttributeName: postId
            AttributeType: S
          - AttributeName: userId
            AttributeType: S
          - AttributeName: createdAt
            AttributeType: S
        KeySchema:
          - AttributeName: postId
            KeyType: HASH
          - AttributeName: userId
            KeyType: RANGE
        BillingMode: PAY_PER_REQUEST
        TableName: ${self:provider.environment.POSTS_TABLE}
        GlobalSecondaryIndexes:
          - IndexName: ${self:provider.environment.USER_ID_INDEX}
            KeySchema:
              - AttributeName: userId
                KeyType: HASH
              - AttributeName: public
                KeyType: RANGE
            Projection:
              ProjectionType: ALL

推荐答案

综合排序键指的是如何使用排序键.

Composite sort keys refer to how you use your sort keys.

您在serverless.yml中唯一需要做的就是定义排序键名称(在此完成).是否使用该排序键作为复合排序键取决于您的应用程序.您可以将排序键的值设置为复合值(例如CA#BEVERLYHILLS#90210),也可以不将其设置为(例如BEVERLYHILLS).DynamoDB或Serverless Framework都不在乎,它更多地是关于如何在应用程序逻辑中使用键的.

The only thing you need to do in your serverless.yml is to define the sort key name (which you've done here). Whether or not you use that sort key as a composite sort key (or not) is up to your application. You'd either set the value of the sort key to a composite value (e.g. CA#BEVERLYHILLS#90210) or not (e.g. BEVERLYHILLS). Neither DynamoDB or Serverless Framework really care, it's more about how you use the keys in your application logic.

请记住,DynamoDB允许您在单个表中存储多个实体.这些实体可能具有不同的主键(分区键和排序键).因此,建议您命名分区键PK和排序键SK.

Keep in mind that DynamoDB allows you to store multiple entities in a single table. These entities may have different primary keys (partition keys and sort keys). For that reason, I'd recommend naming your partition key PK and your sort key SK.

例如,假设您将Post和User实体定义为具有以下主键:

For example, lets say you define your Post and User entities to have the following primary keys:

             Partition Key            Sort Key
Post        POST#<post_id>           <timestamp>
User        USER#<user_id>           USER#<user_id>

在发布的情况下,您的排序键可以是时间戳记(发布发布的时间).对于用户,排序键可以是用户的ID.那么在定义表时,您如何命名排序键字段?

In the case of the Post, your sort key could be a timestamp (the time the post was made). For Users, the sort key could be the ID of the user. So what do you name the sort key field when defining your table?

我喜欢将排序键SK命名为提醒您,您正在使用该字段来组织数据.当然,您可以随意命名,但是如果您将其命名为 userId ,就像您在 serverless.yml 文件中所做的那样,您可能会忘记该字段可以包含用户ID以外的其他内容!

I like naming the sort key SK as a reminder that you are using that field to organize your data. Of course, you can name it whatever you'd like, but if you name it userId, as you've done in your serverless.yml file, you may forget that the field can hold something other than user IDs!

这篇关于使用无服务器框架为dynamodb设置复合排序键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 06:50