简体   繁体   English

如何编写 aws dynamodb 扫描过滤器

[英]how to write aws dynamodb scan filter

I have one dynamodb table and one record is like belwo:我有一个 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

}

How can I write one aws dynamodb scan/query filter with aws cli to just get aTypeId and aId when aTypeId is "test"?当 aTypeId 为“test”时,如何使用 aws cli 编写一个 aws dynamodb 扫描/查询过滤器以获取 aTypeId 和 aId? And

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

I have tried below but no lucky with it我在下面尝试过,但没有运气

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

You field is not in a key or GSI (Global secondary index), then I think you have to use scan method to get object by aTypeId ,您的字段不在键或 GSI(全局二级索引)中,那么我认为您必须使用scan方法通过aTypeId获取对象,

The query will like:查询将类似于:

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

If you get back result with LastEvaluatedKey value, this mean you need take one or more query to get all data:如果您返回带有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"}}'

But, I recommended that create a GSI with aTypeId is hash key will be better.但是,我建议创建一个带有aTypeId的 GSI 是哈希键会更好。

Since aId is a Primary Key and you are trying to search for it, you can only scan through the table (which is might be very expensive operation).由于aId是主键并且您正在尝试搜索它,因此您只能扫描表(这可能是非常昂贵的操作)。 Try below command to scan the item尝试以下命令来扫描项目

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

As a precaution, a single Scan request will only return up to 1MB size of data.作为预防措施,单个Scan请求最多只能返回 1MB 大小的数据。 If there is anymore data to scan, the response from the request will append LastEvaluatedKey and that tells you to run Scan request again to find the item, but this time with --starting-token and the value is exactly LastEvaluatedKey如果还有数据要扫描,请求的响应将附加LastEvaluatedKey并告诉您再次运行Scan请求以查找项目,但这次使用--starting-token并且值正是LastEvaluatedKey

If this type of operation is a routine in your case, it will be better to use Global Secondary Index and use aTypeId as Primary Key如果这种类型的操作在你的情况下是例行公事,最好使用Global Secondary Index并使用aTypeId作为主键

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

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