admin管理员组

文章数量:1332345

I'm learning Dynamodb and for that I installed the local server that es with a shell at http://localhost:8000/shell

now.. I created the following table:

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);

so I created a table only with one hash key called up_time, that's actually the only item in the table.

Now I want to fetch the last 10 inserted up times.

so far I created the following code:

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

});

ok.. so few things to notice:

  1. I don't really need a KeyCondition. i just want the last 10 items, so I used Limit 10 for the limit and ScanIndexForward:false for reverse order.
  2. != or NE are not supported in key expressions for hash keys. and it seems that I must use some kind of index in the query.. confused about that.

so.. any information regarding the issue would be greatly appreciated.

I'm learning Dynamodb and for that I installed the local server that es with a shell at http://localhost:8000/shell

now.. I created the following table:

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);

so I created a table only with one hash key called up_time, that's actually the only item in the table.

Now I want to fetch the last 10 inserted up times.

so far I created the following code:

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

});

ok.. so few things to notice:

  1. I don't really need a KeyCondition. i just want the last 10 items, so I used Limit 10 for the limit and ScanIndexForward:false for reverse order.
  2. != or NE are not supported in key expressions for hash keys. and it seems that I must use some kind of index in the query.. confused about that.

so.. any information regarding the issue would be greatly appreciated.

Share Improve this question asked Mar 13, 2016 at 15:14 ufkufk 32.1k74 gold badges252 silver badges414 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Some modern terminology: Hash is now called Partition, Range is now called Sort.
Thank you Amazon.

You need to understand that Query-ing is an action on hash-keys. In order to initiate a query you must supply a hash-key. Since your table's primary key is only hash key (and not hash+range) you can't query it. You can only Scan it in order to find items. Scan doesn't require any knowledge about items in the table.

Moving on.. when you say "last 10 items" you actually do want a condition because you are filtering on the date attribute, you haven't defined any index so you can't have the engine provide you 10 results. If it were a range key element, you could get the Top-10 ordered elements by querying with a backwards index (ScanIndexForward:false) - again, not your schema.

In your current table - what exactly are you trying to do? You currently only have one attribute which is also the hash key so 10 items would look like (No order, no duplicates):

12312
53453
34234
123
534534
3101
11

You could move those to range key and have a global hash-key "stub" just to initiate the query you're making but that breaks the guidelines of DynamoDB as you have a hot partition and it won't have the best performance. Not sure this bothers you at the moment, but it is worth mentioning.

本文标签: javascriptquery for last 10 items in dynamodb shellStack Overflow