2017-05-07 64 views
1

我有从不同服务/源填充的DynamoDB表。该表具有下一个架构:在dynamoDB中更改列的类型的最佳方法

{ 
    "Id": 14782, 
    "ExtId": 1478240974, //pay attention it is Number 
    "Name": "name1" 
} 

有时,服务开始工作后,我发现一个服务以不正确的格式发送数据。它看起来像:

{ 
    "Id": 14782, 
    "ExtId": "1478240974", //pay attention it is String 
    "Name": "name1" 
} 

DynamoDB是NoSQL数据库所以,现在我有百万混合记录,很难查询或扫描。我明白我的主要错误是遗漏验证。

现在我必须抛出所有记录,如果它是不恰当的类型 - 删除它并添加相同的数据,但具有正确的格式。是否有可能以另一种优雅的方式来完成?

回答

0

所以这很容易。可以使用attribute_type方法。

首先,我加了进口:

from boto3.dynamodb.conditions import Attr 
import boto3 

而且我的代码:

attr = Attr('ExtId').attribute_type('S') 
    response = table.scan(FilterExpression = attr) 
    items = response['Items'] 

    while 'LastEvaluatedKey' in response: 
     response = table.scan(FilterExpression = attr, ExclusiveStartKey = response['LastEvaluatedKey']) 
     items.extend(response['Items']) 

这是有可能找到在下一篇文章中详细状况定制 - DynamoDB Customization Reference

相关问题