Dynamodb 按排序顺序扫描 [英] Dynamodb scan in sorted order
问题描述
我有一个 dynamodb 表.我希望该服务返回此表中的所有项目,并且顺序是按一个属性排序.
Hi I have a dynamodb table. I want the service to return me all the items in this table and the order is by sorting on one attribute.
我需要为此创建全局二级索引吗?如果是这样,散列键应该是什么,范围键是什么?(注意gsi查询必须在GSI的hash key上指定一个EQ"比较器.)
Do I need to create a global secondary index for this? If that is the case, what should be the hash key, what is the range key? (Note that query on gsi must specify a "EQ" comparator on the hash key of GSI.)
非常感谢!
二本
推荐答案
如果您知道 HashKey,那么任何查询都将返回按 Range 键排序的项目.来自文档:
If you know the HashKey, then any query will return the items sorted by Range key. From the documentation:
查询结果总是按范围键排序.如果范围键的数据类型为Number
,则按数字顺序返回结果.否则,按 UTF-8 字节顺序返回结果.默认情况下,排序顺序是升序.要反转顺序,请将 ScanIndexForward
参数设置为 false
.
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 UTF-8 bytes. By default, the sort order is ascending. To reverse the order, set theScanIndexForward
parameter set tofalse
.
现在,如果您需要返回所有项目,您应该使用扫描.您无法对扫描结果进行排序.
Now, if you need to return all the items, you should use a scan. You cannot order the results of a scan.
另一种选择是使用 GSI(示例).在这里,您会看到 GSI 仅包含 HashKey.结果我猜会按照这个key的顺序排列(我还没有在程序中检查过这部分!).
Another option is to use a GSI (example). Here, you see that the GSI contains only HashKey. The results I guess will be in sorted order of this key (I didn't check this part in a program yet!).
这篇关于Dynamodb 按排序顺序扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!