如何编写aws动态数据库扫描滤波器



我有一个发电机数据库表,其中一条记录类似于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-dynadb扫描/查询过滤器来获得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作为主键

最新更新