简体   繁体   English

在两个时间戳之间进行扫描\\查询

[英]scan\query between two timestamps

I'm writing a nodejs 5.7.1 application with aws-sdk for DynamoDB. 我正在使用Aws-SDK为DynamoDB编写一个nodejs 5.7.1应用程序。

I have a table of events that I created with the following code: 我有一个使用以下代码创建的事件表:

var statsTableName='bingodrive_statistics';
var eventNameColumn = 'event_name';
var eventTimeColumn = 'event_time';
var eventDataColumn = 'event_data';
var params = {
    TableName: statsTableName,
    KeySchema: [ // The type of of schema.  Must start with a HASH type, with an optional second RANGE.
        { // Required HASH type attribute
            AttributeName: eventNameColumn,
            KeyType: 'HASH',
        },
        { // Optional RANGE key type for HASH + RANGE tables
            AttributeName: eventTimeColumn,
            KeyType: 'RANGE',
        }
    ],
    AttributeDefinitions: [ // The names and types of all primary and index key attributes only
        {
            AttributeName: eventNameColumn,
            AttributeType: 'S', // (S | N | B) for string, number, binary
        },
        {
            AttributeName: eventTimeColumn,
            AttributeType: 'N'
        }
    ],
    ProvisionedThroughput: { // required provisioned throughput for the table
        ReadCapacityUnits: 1,
        WriteCapacityUnits: 1,
    }
};
dynamodbClient.createTable(params, callback);

as you can see, I have a Hash + Range index. 如您所见,我有一个Hash + Range索引。 the range is on event_time. 范围是在event_time上。

now I want to scan or query for all the items between two specific dates. 现在,我想扫描或查询两个特定日期之间的所有项目。

so i'm sending the following params to the query function of dynamoDb: 因此,我将以下参数发送到dynamoDb的查询功能:

{
  "TableName": "bingodrive_statistics",
  "KeyConditionExpression": "event_time BETWEEN :from_time and :to_time",
  "ExpressionAttributeValues": {
    ":from_time": 1457275538691,
    ":to_time": 1457279138691
}

and i'm getting this error: 我收到此错误:

{
  "message": "Query condition missed key schema element",
  "code": "ValidationException",
  "time": "2016-03-06T15:46:06.862Z",
  "requestId": "5a672003-850c-47c7-b9df-7cd57e7bc7fc",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 0 
} 

I'm new to dynamoDb. 我是dynamoDb的新手。 I don't know what's the best method, Scan or Query in my case. 我不知道最好的方法是“扫描”还是“查询”。 any information regarding the issue would be greatly appreciated. 任何有关此问题的信息将不胜感激。

You should use query . 您应该使用query You can't use only range key if you want to query for values between two range keys, you need to use hash key as well since range key. 如果要查询两个范围键之间的值,则不能使用范围键,因为范围键也需要使用哈希键。 It's because hash key (partition key) is used to select a physical partition where the data is stored, sorted by range key (sort key). 这是因为哈希键(分区键)用于选择存储数据的物理分区,并按范围键(排序键)进行排序。 From DynamoDB developer guide : DynamoDB开发人员指南

If the table has a composite primary key (partition key and sort key), DynamoDB calculates the hash value of the partition key in the same way as described in Data Distribution: Partition Key—but it stores all of the items with the same partition key value physically close together, ordered by sort key value. 如果表具有复合主键(分区键和排序键),则DynamoDB以与数据分发:分区键中所述相同的方式计算分区键的哈希值,但是它存储具有相同分区键的所有项目值在物理上靠在一起,并按排序键值排序。

Also, you should choose partition key that distributes well your data. 另外,您应该选择分区键,以很好地分配数据。 If evenName has small total number of values, it might not be the best option (See Guidelines For Tables ] 如果evenName的值总数很小,则可能不是最佳选择(请参见表准则

That said, if you already have eventName as your hash key and eventTime as your range Key, you should query (sorry for pseudo code, I use DynamoDBMapper normally): 就是说,如果您已经有eventName作为您的哈希键和eventTime作为您的范围键,则应该进行查询(对不起,伪代码,我通常使用DynamoDBMapper ):

hashKey = name_of_your_event
conditions = BETWEEN
  attribute_values (eventTime1, eventTime2)

You don't need additional Local Secondary Index or Global Secondary Index for that. 您不需要额外的本地二级索引全局二级索引 Note that GSI let's you query for columns that are not indexed with the table hash and range key, but to query data between the timestamps, you will still need a range key or will need to do a Scan otherwise. 请注意,GSI让您查询未使用表哈希和范围键索引的列,但要查询时间戳之间的数据,您仍将需要范围键,否则将需要执行扫描

Use this query 使用此查询

function getConversationByDate(req , cb) {

var payload = req.all; //05/09/2017
var params = {
    TableName: "message",
    IndexName: "thread_id-timestamp-index",
    KeyConditionExpression: "#mid = :mid AND #time BETWEEN :sdate AND :edate",
    ExpressionAttributeNames: {
        "#mid": "thread_id",
        "#time": "timestamp"
    },
    ExpressionAttributeValues: {
        ":mid": payload.thread_id,
        ":sdate": payload.startdate,
        ":edate": payload.enddate
    }
};
req.dynamo.query(params, function (err, data) {
    cb(err, data);
    });
}

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

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