首页 > 解决方案 > 如何编写aws dynamodb扫描过滤器

问题描述

我有一个 dynamodb 表,一个记录就像下面这样:

{
    "Items": [
        {
            "toObjectId": {
                "S": "5678"
            },         
            "keyValue": {
                "S": "7890"
            },
            "aTypeId": {
                "S": "test"
            },
            "aws:rep:deleting": {
                "BOOL": false
            },
            "currentAt": {
                "N": "1582476260000"
            },
            "keyTypeId": {
                "S": "test2"
            },
            "aId": {
                "S": "1234"
            },
            "aTypeId_keyTypeId_keyValue_currentAt": {
                "S": "test_test2_7890_1582476260000"
            },
            "fromObjectId": {
                "S": "12345"
            },

        }
    ],
    "Count": 2562,
    "ScannedCount": 2562,
    "ConsumedCapacity": null

}

当aTypeId为“test”时,如何使用aws cli编写一个aws dynamodb扫描/查询过滤器来获取aTypeId和aId?和

Primary partition key is aId (String)
Primary sort key is aTypeId_keyTypeId_keyValue_currentAt (String) 

我在下面尝试过,但没有运气

aws dynamodb query \
    --table-name test \
    --key-condition-expression "aTypeId = :aTypeId" \
    --expression-attribute-values '{
        ":aTypeId": { "S": "test" }     
    }' 

标签: amazon-web-servicesamazon-dynamodb

解决方案


您的字段不在键或 GSI(全局二级索引)中,那么我认为您必须使用scan方法来获取对象aTypeId

查询将喜欢:

aws dynamodb scan \
     --table-name test \
     --filter-expression "aTypeId = :typeValue" \
     --projection-expression "aTypeId, aId" \
     --expression-attribute-values '{":typeValue":{"S":"test"}}'

如果您返回带有LastEvaluatedKey值的结果,这意味着您需要进行一个或多个查询来获取所有数据:

aws dynamodb scan \
     --table-name test \
     --filter-expression "aTypeId = :typeValue" \
     --projection-expression "aTypeId, aId" \
     --starting-token VALUE_NEXT_TOKEN_OF_LAST_QUERY \
     --expression-attribute-values '{":typeValue":{"S":"test"}}'

但是,我建议使用aTypeIdis hash key 创建 GSI 会更好。


推荐阅读