python - aws cli dynamodb(ValidationException)错误

标签 python amazon-web-services amazon-dynamodb aws-cli

我正在寻找使用 python 的 boto3 模块将项目批量写入 dynamodb 并且我得到了这个。这是我第一次使用 aws cli 或 boto3。文档说当存在空值和可能不正确的数据类型时会发生验证异常错误,但我已经玩过所有这些并且它似乎不起作用。

dynamodb 一次只喜欢写入 25 个项目吗?如果是这样,我该如何控制这些批处理?

我的要求:

client = boto3.client('dynamodb')
response = client.batch_write_item(RequestItems=batch_dict)

batch_dict 的顶部:

{'scraper_exact_urls': [{'PutRequest': {'Item': {'Sku': {'S': 'T104P3'},
 'pps_id': {'N': '427285976'},
 'scraper_class_name': {'S': 'scraper_class_name'},
 'store_id': {'N': '1197386754'},
 'updated_by': {'S': 'user'},
 'updated_on': {'N': '1480714223'},
 'updated_url': {'S': 'http://www.blah.com'}}}},
 {'PutRequest': {'Item': {'Sku': {'S': 'T104P3'},
 'pps_id': {'N': '427285976'},
 'scraper_class_name': {'S': 'scraper_class_name'},
 'store_id': {'N': '1197386754'},
 'updated_by': {'S': 'user'},
 'updated_on': {'N': '1480714223'},
 'updated_url': {'S': 'http://www.blah.com'}}}},....

架构:

属性: "pps_id"=>\Aws\DynamoDb\Enum\Type::NUMBER, “sku”=>\Aws\DynamoDb\Enum\Type::STRING, “scraper_class_name”=>\Aws\DynamoDb\Enum\Type::STRING, "store_id"=>\Aws\DynamoDb\Enum\Type::NUMBER, “updated_url”=>\Aws\DynamoDb\Enum\Type::STRING, "updated_by"=>\Aws\DynamoDb\Enum\Type::STRING, “updated_on”=>\Aws\DynamoDb\Enum\Type::NUMBER, 领域: "pps_id", “scraper_class_name”,

错误:

ClientError: An error occurred (ValidationException) when calling the    BatchWriteItem operation: 1 validation error detected: Value .... Map value   must satisfy constraint: [Member must have length less than or equal to 25,   Member must have length greater than or equal to 1]

最佳答案

BatchWriteItem API 一次处理 25 个项目。您可以使用以下代码,改编自 non-copying batching question , 在 25 个项目 block 上调用 BatchWriteItem

def batch(iterable, n=1):
    l = len(iterable)
    for ndx in range(0, l, n):
        yield iterable[ndx:min(ndx + n, l)]

client = boto3.client('dynamodb')

for x in batch(batch_dict['scraper_exact_urls'], 25):
    subbatch_dict = {'scraper_exact_urls': x}
    response = client.batch_write_item(RequestItems=subbatch_dict)

关于python - aws cli dynamodb(ValidationException)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40941231/

相关文章:

node.js - 带有 AWS Lambda 错误 "Cannot find module"的无服务器框架

json - 我收到 "Invalid template property or properties"模板验证错误 : Invalid template property or properties [IPAssoc, IPAddress]”

python - 什么时候使用 dynamodb.client、dynamodb.resource 和 dynamodb.Table?

python - 最近启动的 Google Compute Engine VM 的非确定性连接成功

python - 阿拉伯文本不仅在 lxml 输出中显示为字符实体

python - 用python递归实现冰雹序列或Collat​​z猜想的数学难题?

java - 使用 wavemaker 和亚马逊产品广告 API 的经验?

c# - 如何使用 ServiceStack.Aws 在 DynamoDb 中将 enum 作为 int 插入

node.js - Vogels 是使用 Nodejs 的 Amazon dynamodb 映射器的不错选择吗?

python - py.test 和 fixtures - 如何只选择一种参数变体