Dynamodb扫描按排序顺序 [英] Dynamodb scan in sorted order
问题描述
你好,我有一个动态表。我想让服务返回此表中的所有项目,并且顺序是通过对一个属性进行排序。
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的哈希键上指定一个 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.)
非常感谢!
Erben
推荐答案
如果您知道HashKey,则任何查询都将返回排序方式为范围键。从文档:
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
.
现在,如果您需要退回 all 个项目,则应使用扫描程序。您无法订购扫描结果。
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。我猜结果将按此键排序(我尚未在程序中检查此部分!)。
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屋!