在dynamodb shell中查询最后10个项目

ufk*_*ufk 1 javascript node.js amazon-dynamodb

我正在学习Dynamodb,为此,我在http:// localhost:8000 / shell上安装了带有shell的本地服务器

现在..我创建了下表:

var serverUpTimeTableName = 'bingodrive_server_uptime';
var eventUpTimeColumn = 'up_time';

var params = {
    TableName: serverUpTimeTableName,
    KeySchema: [ // The type of of schema.  Must start with a HASH type, with an optional second RANGE.
        { // Required HASH type attribute
            AttributeName: eventUpTimeColumn,
            KeyType: 'HASH',
        },
    ],
   AttributeDefinitions: [ // The names and types of all primary and index key attributes only
        {
            AttributeName: eventUpTimeColumn,
            AttributeType: 'N', // (S | N | B) for string, number, binary
        },
    ],
    ProvisionedThroughput: { // required provisioned throughput for the table
        ReadCapacityUnits: 2,
        WriteCapacityUnits: 2,
    }
};
dynamodb.createTable(params, callback);
Run Code Online (Sandbox Code Playgroud)

因此,我仅使用一个名为up_time的哈希键创建了一个表,这实际上是表中的唯一项。

现在,我想获取最近插入的10次时间。

到目前为止,我创建了以下代码:

var serverUpTimeTableName = 'bingodrive_server_uptime';

var eventUpTimeColumn = 'up_time';

var params = {
    TableName: serverUpTimeTableName,
    KeyConditionExpression: eventUpTimeColumn + ' != :value',
    ExpressionAttributeValues: {
        ':value':0
    },
    Limit: 10,
    ScanIndexForward: false
}
docClient.query(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response

});
Run Code Online (Sandbox Code Playgroud)

好..要注意的几件事:

  1. 我真的不需要KeyCondition。我只想要最后10个项目,所以我使用Limit 10了限制和ScanIndexForward:false反向订单。
  2. !=NE在哈希键的键表达式中不受支持。似乎我必须在查询中使用某种索引..对此感到困惑。

因此,有关此问题的任何信息将不胜感激。

Che*_*rel 6

一些现代术语:哈希现在称为分区,范围现在称为排序。
谢谢亚马逊。

您需要了解Query-ing是对哈希键的操作。为了启动查询,您必须提供一个哈希键。由于表的主键仅是哈希键(而不是哈希+范围),因此无法查询它。您只能用Scan它来查找项目。扫描不需要任何有关表中项目的知识。

继续..当您说“最后10个项目”时,实际上您确实想要一个条件,因为您要过滤date属性,因此您尚未定义任何索引,因此无法让引擎为您提供10个结果。如果它是范围键元素,则可以通过使用向后索引(ScanIndexForward:false)查询来获得前10位有序元素-再次,不是您的模式。

在您当前的表格中-您到底想做什么?您目前只有一个属性,它也是哈希键,因此10个项目看起来像(无顺序,无重复):

12312
53453
34234
123
534534
3101
11
Run Code Online (Sandbox Code Playgroud)

您可以将它们移动到range键,并具有全局哈希键“ stub”,仅用于启动您要进行的查询,但是由于您具有热分区,因此这违反了DynamoDB的准则,并且它不会具有最佳性能。目前尚不确定这会打扰您,但是值得一提。