如何使用 AWSDynamoDBQueryExpression 获取十个最近添加的项目?
How to get ten of recently added items using AWSDynamoDBQueryExpression?
我想使用 AWSDynamoDBQueryExpression 获取十个最近添加的项目(按创建日期排序)。这是我的代码:
AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
AWSDynamoDBQueryExpression *queryExpression = [AWSDynamoDBQueryExpression new];
queryExpression.keyConditionExpression = @"userId = :authorName AND creationDate > :creationDateVal";
queryExpression.expressionAttributeValues = @{@":authorName": @"me", @":creationDateVal": @0};
queryExpression.limit = @10;
[[dynamoDBObjectMapper query:[Post class]
expression:queryExpression]
continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(@"The request failed. Error: [%@]", task.error);
}
if (task.exception) {
NSLog(@"The request failed. Exception: [%@]", task.exception);
}
if (task.result) {
AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
NSLog(@"paginatedOutput = %@", paginatedOutput.items);
}
return nil;
}];
我得到的十件物品有限,但这些物品是我添加到 table 中的第一件,而不是最后一件。
如果您试图通过排序键(即上述场景中的 creationDate)获取最后 10 个项目,您可以使用 ScanIndexForward = false (Descending Order).
ScanIndexForward — (Boolean) Specifies the order for index traversal:
If true (default), the traversal is performed in ascending order; if
false, the traversal is performed in descending order.
Items with the same partition key value are stored in sorted order by
sort key. If the sort key data type is Number, the results are stored
in numeric order. For type String, the results are stored in order of
ASCII character code values. For type Binary, DynamoDB treats each
byte of the binary data as unsigned.
If ScanIndexForward is true, DynamoDB returns the results in the order
in which they are stored (by sort key value). This is the default
behavior. If ScanIndexForward is false, DynamoDB reads the results in
reverse order by sort key value, and then returns the results to the
client.
限制注意事项:-
需要一直查询到LastEvaluatedKey为null。
Limit — (Integer) The maximum number of items to evaluate (not
necessarily the number of matching items). If DynamoDB processes the
number of items up to the limit while processing the results, it stops
the operation and returns the matching values up to that point, and a
key in LastEvaluatedKey to apply in a subsequent operation, so that
you can pick up where you left off. Also, if the processed data set
size exceeds 1 MB before DynamoDB reaches this limit, it stops the
operation and returns the matching values up to the limit, and a key
in LastEvaluatedKey to apply in a subsequent operation to continue the
operation. For more information, see Query and Scan in the Amazon
DynamoDB Developer Guide.
我想使用 AWSDynamoDBQueryExpression 获取十个最近添加的项目(按创建日期排序)。这是我的代码:
AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
AWSDynamoDBQueryExpression *queryExpression = [AWSDynamoDBQueryExpression new];
queryExpression.keyConditionExpression = @"userId = :authorName AND creationDate > :creationDateVal";
queryExpression.expressionAttributeValues = @{@":authorName": @"me", @":creationDateVal": @0};
queryExpression.limit = @10;
[[dynamoDBObjectMapper query:[Post class]
expression:queryExpression]
continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(@"The request failed. Error: [%@]", task.error);
}
if (task.exception) {
NSLog(@"The request failed. Exception: [%@]", task.exception);
}
if (task.result) {
AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
NSLog(@"paginatedOutput = %@", paginatedOutput.items);
}
return nil;
}];
我得到的十件物品有限,但这些物品是我添加到 table 中的第一件,而不是最后一件。
如果您试图通过排序键(即上述场景中的 creationDate)获取最后 10 个项目,您可以使用 ScanIndexForward = false (Descending Order).
ScanIndexForward — (Boolean) Specifies the order for index traversal: If true (default), the traversal is performed in ascending order; if false, the traversal is performed in descending order.
Items with the same partition key value are stored in sorted order by sort key. If the sort key data type is Number, the results are stored in numeric order. For type String, the results are stored in order of ASCII character code values. For type Binary, DynamoDB treats each byte of the binary data as unsigned.
If ScanIndexForward is true, DynamoDB returns the results in the order in which they are stored (by sort key value). This is the default behavior. If ScanIndexForward is false, DynamoDB reads the results in reverse order by sort key value, and then returns the results to the client.
限制注意事项:-
需要一直查询到LastEvaluatedKey为null。
Limit — (Integer) The maximum number of items to evaluate (not necessarily the number of matching items). If DynamoDB processes the number of items up to the limit while processing the results, it stops the operation and returns the matching values up to that point, and a key in LastEvaluatedKey to apply in a subsequent operation, so that you can pick up where you left off. Also, if the processed data set size exceeds 1 MB before DynamoDB reaches this limit, it stops the operation and returns the matching values up to the limit, and a key in LastEvaluatedKey to apply in a subsequent operation to continue the operation. For more information, see Query and Scan in the Amazon DynamoDB Developer Guide.