我正在使用dynamoDb存储一些额外的信息,但我有一些麻烦排序我的数据 .
我有以下创建语法,其中有一个 storeId
和 number
键 . 我将 number
设置为排序键,但问题是数据根本没有排序 .
$response = $dynamoDb->createTable([
'TableName' => 'foo',
'KeySchema' => [
[
'AttributeName' => 'storeId',
'KeyType' => 'HASH' //Partition key
],
[
'AttributeName' => 'number',
'KeyType' => 'RANGE' // sort Key
]
],
'AttributeDefinitions' => [
[
'AttributeName' => 'storeId',
'AttributeType' => 'N'
],
[
'AttributeName' => 'number',
'AttributeType' => 'N'
]
],
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 20,
'WriteCapacityUnits' => 20
]
]);
我的扫描参数:
$scanParams = [
'TableName' => 'foo',
'ProjectionExpression' => '#storeId, #number',
'FilterExpression' => '#number >= :n',
'ExpressionAttributeNames'=> [ '#storeId' => 'storeId', '#number' => 'number'],
'ExpressionAttributeValues' => [
':n' => ['N' => $number]
]
];
我的扫描结果:
StoreId number
68001 80000
25000 37000
463501 527000
4800001 5300000
360001 400000
2000001 2600000
如您所见,数据未按数字属性排序 .
1 回答
请阅读下面的第一段 . 排序键用于存储物理上靠近在一起的相同分区键值的所有项目,并且默认按升序排序(即重要的一点是数据不在分区键上排序) . 换句话说,对于相同的分区键,数据默认按升序排序 .
Example:-
Partition Key Sort Key
p1,s1
p1,s2
p1,s3
p2,s1
p2,s2
在Query API中,有一个参数可以按升序或降序获取结果 .
ScanIndexForward — (Boolean)
指定索引遍历的顺序:如果为true(默认值),则以递增顺序执行遍历;如果为false,则以递减顺序执行遍历 .
具有相同分区键值的项目按排序键按排序顺序存储 . 如果排序键数据类型为Number,则结果将按数字顺序存储 . 对于String类型,结果按ASCII字符代码值的顺序存储 . 对于Binary类型,DynamoDB将二进制数据的每个字节视为无符号 .
如果ScanIndexForward为true,DynamoDB将按照存储顺序返回结果(按排序键值) . 这是默认行为 . 如果ScanIndexForward为false,则DynamoDB按排序键值以相反顺序读取结果,然后将结果返回给客户端 .
Query API