繁体   English   中英

如何编写 aws dynamodb 扫描过滤器

[英]how to write aws dynamodb scan filter

我有一个 dynamodb 表和一个记录就像 belwo:

{
    "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" }     
    }' 

您的字段不在键或 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"}}'

但是,我建议创建一个带有aTypeId的 GSI 是哈希键会更好。

由于aId是主键并且您正在尝试搜索它,因此您只能扫描表(这可能是非常昂贵的操作)。 尝试以下命令来扫描项目

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

作为预防措施,单个Scan请求最多只能返回 1MB 大小的数据。 如果还有数据要扫描,请求的响应将附加LastEvaluatedKey并告诉您再次运行Scan请求以查找项目,但这次使用--starting-token并且值正是LastEvaluatedKey

如果这种类型的操作在你的情况下是例行公事,最好使用Global Secondary Index并使用aTypeId作为主键

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM