在 AWS DynamoDb 上查询 Range 键的最大值 [英] Querying for greatest value of Range key on AWS DynamoDb
问题描述
什么是 DynamoDB 等价物
What is the DynamoDB equivalent of
SELECT MAX(RANGE_KEY) FROM MYTABLE WHERE PRIMARYKEY = "value"
我能想到的最好的是
from boto.dynamodb2.table import Table as awsTable
tb = awsTable("MYTABLE")
rs = list(tb.query_2(PRIMARYKEY__eq="value", reverse=True, limit=1))
MAXVALUE = rs[0][RANGE_KEY]
有没有更好的方法来做到这一点?
Is there a better way to do this?
推荐答案
这是正确的方法.
因为Hash Key匹配的记录是按照Range Key排序的,所以按照后代顺序获取第一个会得到最大范围key的记录.
Because the records matched by the Hash Key are sorted by the Range Key, getting the first one by the descendant order will give you the record with the maximum range key.
查询结果始终按范围键排序.如果数据类型为range键为Number,结果按数字顺序返回;否则,结果按 ASCII 字符码的顺序返回价值观.默认情况下,排序顺序是升序.颠倒顺序使用设置为 false 的 ScanIndexForward 参数.
Query results are always sorted by the range key. If the data type of the range key is Number, the results are returned in numeric order; otherwise, the results are returned in order of ASCII character code values. By default, the sort order is ascending. To reverse the order use the ScanIndexForward parameter set to false.
查询和扫描操作 - Amazon DynamoDB:http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html
Query and Scan Operations - Amazon DynamoDB : http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html
注意:通过 boto API 将 reverse
参数设置为 true 等效于通过原生 AWS API 将 ScanIndexForward
设置为 false.
NOTE: Setting the reverse
parameter to true via boto API is equivalent to setting ScanIndexForward
to false via the native AWS API.
这篇关于在 AWS DynamoDb 上查询 Range 键的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!