【问题标题】:aws cli dynamo db (ValidationException) Erroraws cli 发电机数据库(ValidationException)错误
【发布时间】:2017-04-17 21:31:26
【问题描述】:

我正在寻找使用 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]

【问题讨论】:

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


【解决方案1】:

BatchWriteItem API 一次可处理 25 个项目。您可以使用以下代码(改编自 non-copying batching question)在 25 个项目块上调用 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 用户来说,它是不完整的代码
【解决方案2】:

这里是批量写入/删除许多项目的Javascript版本,以防有人需要:

const batchWriteManyItems = async (tableName, itemObjs, chunkSize = 25) => {

        const buildParams = (table) => JSON.parse(`{"RequestItems": {"${table}": []}}`)

        const queryChunk = (arr, size) => {
            const tempArr = []
            for (let i = 0, len = arr.length; i < len; i += size) {
                tempArr.push(arr.slice(i, i + size));
            }

            return tempArr
        }

        await Promise.all(queryChunk(itemObjs, chunkSize).map(async chunk => {
            let itemParams = buildParams(tableName);
            itemParams.RequestItems[tableName] = chunk
            await dynamoDB.batchWriteItem(itemParams).promise()
        }))
    }

注意:itemObjs 属性可以使用{PutRequest: {Item: ...} 用于写入项目,{DeleteRequest: {Item: ...} 用于删除项目

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 2022-07-07
    • 2020-06-18
    相关资源
    最近更新 更多