简体   繁体   English

aws dynamodb - 如何按日期查询排序键

[英]aws dynamodb - how to query by date for sort key

I want to query the table and get data which is after certain date.我想查询表并获取特定日期之后的数据。

Hash Key: id Hash 密钥:id
Sort Key: timestamp排序键:时间戳

error错误

ValidationException: Query condition missed key schema element: id

index.js索引.js

var aws = require('aws-sdk');
const dynamodb = new aws.DynamoDB();

exports.handler = async (event, context, callback) => {
  const documentClient = new aws.DynamoDB.DocumentClient();

  const params = {
    TableName : 'dynamodb-log-testing',
    KeyConditionExpression: '#myTimestamp >= :myDate',
    ExpressionAttributeNames: {
      "#myTimestamp": "timestamp"
    },
    ExpressionAttributeValues: {   
        ':myDate': '2017-11-17'
    }
  };

    try{
        const data = await documentClient.query(params).promise();
        console.log(data)
    }catch(err){
      console.log(err)
    }

};

All queries on a DynamoDB table must include the partition key (aka HASH). DynamoDB 表上的所有查询都必须包含分区键(又名 HASH)。 You can use scan , but it's not recommended for most use cases.您可以使用scan ,但不建议在大多数用例中使用。 You can create a GSI where the partition on the index is a fixed value, and have the same timestamp value for the sort key.您可以创建一个 GSI,其中索引上的分区是固定值,并且排序键具有相同的timestamp值。 That will allow you to query the index in the way you are describing (except that you'll include the fixed value in the query as well).这将允许您以您描述的方式查询索引(除了您还将在查询中包含固定值)。 Your query would look like this:您的查询将如下所示:

var aws = require('aws-sdk');
const dynamodb = new aws.DynamoDB();

exports.handler = async (event, context, callback) => {
  const documentClient = new aws.DynamoDB.DocumentClient();

  const params = {
    TableName : 'dynamodb-log-testing',
    IndexName: 'myGSI1',
    KeyConditionExpression: 'gsi1pk = :fixedValue and #myTimestamp >= :myDate',
    ExpressionAttributeNames: {
      "#myTimestamp": "timestamp"
    },
    ExpressionAttributeValues: {   
        ':myDate': '2017-11-17',
        ':fixedValue': 'some fixed value'
    }
  };

    try{
        const data = await documentClient.query(params).promise();
        console.log(data)
    }catch(err){
      console.log(err)
    }

};

Keep in mind that this model has a strong potential for hot partitions on the GSI if your data is large.请记住,如果您的数据很大,这个 model 在 GSI 上有很大的热分区潜力。 For that reason you may want to rethink the access pattern a bit.出于这个原因,您可能需要重新考虑一下访问模式。 If you can do something like maybe include the date part of the timestamp in the partition and just include the time in the sort that would help.如果您可以做一些事情,比如在分区中包含时间戳的日期部分,并且只包含有助于排序的时间。 That does mean that you can't query across days in a single query.这确实意味着您不能在单个查询中跨天查询。

Best Practices for Designing and Architecting with DynamoDB has a lot of good information on best practices for DynamoDB. Best Practices for Designing and Architecting with DynamoDB有很多关于 DynamoDB 最佳实践的有用信息。

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

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