简体   繁体   English

Dynamo DB 关于表结构的建议

[英]Dynamo DB advice on structure of table

I'm wanting to store data like this我想像这样存储数据

{ date: '2020-10-06T20:46:01.942Z', topic: 'a great topic', votes: 123 },
{ date: '2020-10-07T23:46:01.942Z', topic: 'a great topic', votes: 125 },
{ date: '2020-10-06T20:46:03.942Z', topic: 'a not so great topic', votes: 12 }

I've created a Dynamo and have my timestamp as the primary key, due to low traffic it's safe to assume these will be unique.我创建了一个 Dynamo 并将我的时间戳作为主键,由于流量低,可以安全地假设这些将是唯一的。

I'm hoping to return data based on topic so a dynamo query using the sort key where it equals a great topic .我希望返回基于主题的数据,因此使用排序键的发电机查询等于a great topic

I keep getting "message": "Query condition missed key schema element" when using the following in a dyanmo db query statement:在 dyanmo db 查询语句中使用以下内容时,我不断收到"message": "Query condition missed key schema element"

params = {
    TableName: table,
    KeyConditionExpression: "#topic = :topic",
    ExpressionAttributeNames:{
        "#topic": "topic"
    },
    ExpressionAttributeValues: {
        ":topic": "a great topic"
    }
  }

This is my table这是我的桌子

var tableParams = {
    TableName : "a cool table",
    KeySchema: [       
        { AttributeName: "date", KeyType: "HASH"},  //Partition key
        {
          AttributeName: "topic",
          KeyType: "RANGE"
      }
    ],
    AttributeDefinitions: [       
        { AttributeName: "date", AttributeType: "S" },
        { AttributeName: "topic", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 5, 
        WriteCapacityUnits: 5
    }
};

I'm hesitant to use scan because it can be quite wasteful even though it would be easier to filter the data using javascript :p我对使用 scan 犹豫不决,因为即使使用 javascript 过滤数据会更容易,但它可能会非常浪费:p

Thanks for your help.谢谢你的帮助。

If you query by topic, it would be much more effective to set the topic as partition (hash) key and the timestamp as sort (range) key.如果按主题查询,将主题设置为分区(哈希)键,将时间戳设置为排序(范围)键会更有效。 Otherwise you would always need to provide the specific timestamp because a query always needs to include the partition key.否则,您将始终需要提供特定的时间戳,因为查询始终需要包含分区键。

A beneficial side effect would be that the returned results would already be sorted by timestamp.一个有益的副作用是返回的结果已经按时间戳排序。

Update: For further details, please refer to Seth's great answer above!更新:有关更多详细信息,请参阅上面 Seth 的精彩回答!

You are attempting to query your table using the topic field, which won't work because it's your sort key.您正在尝试使用topic字段查询您的表,这将不起作用,因为它是您的排序键。 When querying DynamoDB, you need to provide the partition key with the optional sort key.查询 DynamoDB 时,您需要提供带有可选排序键的分区键。

Let's quickly review some terminology so we are all on the same page (from the docs ).让我们快速回顾一些术语,以便我们都在同一页面上(来自docs )。

  • Primary Key - The primary key uniquely identifies each item in the table, so that no two items can have the same key.主键 - 主键唯一标识表中的每个项目,因此没有两个项目可以具有相同的键。 There are two different types of Primary Key:有两种不同类型的主键:
  • Partition key – A simple primary key, composed of one attribute known as the partition key.分区键 – 一个简单的主键,由一个称为分区键的属性组成。
  • Partition key and sort key – Referred to as a composite primary key, this type of key is composed of two attributes.分区键和排序键 – 称为复合主键,这种类型的键由两个属性组成。 The first attribute is the partition key, and the second attribute is the sort key第一个属性是分区键,第二个属性是排序键

When you perform a query operation in Dynamodb, you use the KeyConditionExpression parameter to provide a specific value for the partition key.当您在 Dynamodb 中执行query操作时,您使用KeyConditionExpression参数为分区键提供特定值。 The Query operation will return all of the items from the table or index with that partition key value.查询操作将返回表或索引中具有该分区键值的所有项目。 You can optionally narrow the scope of the Query operation by specifying a sort key value and a comparison operator in KeyConditionExpression您可以选择通过在KeyConditionExpression指定排序键值和比较运算符来缩小 Query 操作的范围

Looking at your query, you're trying to query an item by specifying only the sort key, which is not going to work.查看您的查询,您试图通过指定排序键来查询项目,这是行不通的。 I think you've got your primary key backwards.我认为你的主键倒退了。 You want the partition key to be topic and your sort key to be date .您希望分区键为topic ,排序键为date

To get your query to work as written, change the KeySchema of your table to this:要使您的查询按书面方式工作,请将表的KeySchema更改为:

    KeySchema: [       
        { AttributeName: "topic", KeyType: "HASH"},
        { AttributeName: "date", KeyType: "RANGE"}
    ]

If you did this, your query operation would return the items for that topic sorted by date.如果您这样做,您的query操作将返回按日期排序的该主题的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM