范围键项目计数为 0 的 DynamoDB GSI

DynamoDB GSI with range key item count 0

我在 DynamoDB 中有一个 table,看起来像这样:

我在 "Category" 上向 table 添加了一个全局二级索引,它工作正常,并在项目计数下为我提供了 table 中的项目数。

然后我意识到我实际上需要能够在特定的 "Category" 中搜索但按 "UserRating"

排序

所以我删除了 GSI 并制作了一个新的,如下所示:

我认为这一切都很好,正确的类别(字符串)类型和 UserRating 类型(数字)的名称是正确的。

但是在它创建完 GSI 之后,我查看了控制台,它显示项目数为 0,尽管在此测试中应该有 13 个 table,如下图所示:

感谢您的帮助。

找到答案了。我在测试时将我的所有读写容量设置为 1 个单位,一旦我增加它就修复了错误,我可以看到这些项目。

我认为当您在 key-schema 类型和具体项类型之间有一个简单类型 mis-match 时,这种情况更有可能发生。

Managing Global Secondary Indexes-

Note

In some cases, DynamoDB will not be able to write data from the table to the index due to index key violations. This can occur if the data type of an attribute value does not match the data type of an index key schema data type, or if the size of an attribute exceeds the maximum length for an index key attribute. Index key violations do not interfere with global secondary index creation; however, when the index becomes ACTIVE, the violating keys will not be present in the index.

DynamoDB provides a standalone tool for finding and resolving these issues. For more information, see Detecting and Correcting Index Key Violations.

举例

项目看起来像:

    "Items": [
    {
        "Timestamp": {
            "N": "1542475507"
        },
        "DevID": {
            "S": "slfhioh1234oi23lk23kl4h235pjpo235lnsfvuwerfj2roin2l3rn9fj9f8hwen"
        },
        "UID": {
            "S": "1"
        }
    }
],

索引看起来像:

  "GlobalSecondaryIndexes": [
        {
            "IndexName": "UID-Timestamp-index",
            "Projection": {
                "ProjectionType": "KEYS_ONLY"
            },
            "ProvisionedThroughput": {
                "WriteCapacityUnits": 1,
                "ReadCapacityUnits": 1
            },
            "KeySchema": [
                {
                    "KeyType": "HASH", 
                    "AttributeName": "UID"
                },
                {
                    "KeyType": "RANGE",
                    "AttributeName": "Timestamp"
                }
            ], 
        }
    ]

Table 具有属性定义:

"AttributeDefinitions": [
        {
            "AttributeName": "Timestamp",
            "AttributeType": "S"
        },
        {
            "AttributeName": "UID",
            "AttributeType": "S"
        }
    ]

该项目不会出现在您的新索引中。

完全有可能存在类型不匹配(在本例中为 "S" != "N"),而不会在创建时对其进行标记。这是有道理的。你可能想故意做这种事,但如果你不小心做了 - 它不太好

根据 Amazon documentation 这大约每 6 小时更新一次。

ItemCount - The number of items in the global secondary index. DynamoDB updates this value approximately every six hours. Recent changes might not be reflected in this value.

在我的例子中,即使控制台仍然显示 ItemCount 零并且没有为索引的 scan/query 返回任何结果,我还是能够从我的代码中成功查询它。

当索引名称包含破折号时,我也有奇怪的行为(没有结果),如 OP 的屏幕截图所示。用下划线替换破折号解决了我的问题。