程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Boto 3 DynamoDB batchWriteItem指定类型时,属性值类型无效

发布于2019-11-07 20:38     阅读(1872)     评论(0)     点赞(3)     收藏(3)


尝试对DynamoDB表执行batch_write_item时,Python Boto3出现了一个奇怪的问题。我正在关注文档,并尝试编写单件物品。该表已正确设置,我可以通过AWS CLI运行批处理写入项目,没有问题。

假设客户端和DynamoDB设置正确,我运行:

client.batch_write_item(RequestItems={
    "myTable": [
        {
            "PutRequest": {
                "Item": {
                    "name": {
                        "S": "hello"
                    },
                    "value": {
                        "S": "world"
                    }
                }
            }
        }
    ]
})

我收到以下错误:

botocore.exceptions.ClientError:调用BatchWriteItem操作时发生错误(ValidationException):无效的属性值类型

如果更改它,请删除类型并运行:

client.batch_write_item(RequestItems={
    "myTable": [
        {
            "PutRequest": {
                "Item": {
                    "name": "hello",
                    "value": "world"
                }
            }
        }
    ]
})

它按预期工作。

我需要使用遵循文档的前一种格式,并且与AWS cli兼容。

文档是否错误,或者我错过了配置设置,版本问题或其他原因?


解决方案


这也让我很高兴,看起来您正在使用DynamoDB 资源,而不是客户端它们都提供相同的功能,但作用略有不同。这是您要找的东西:

http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#DynamoDB.ServiceResource.batch_write_item

除此之外,文档还不清楚。这是我发现的:

  • 使用资源(当前正在做的事情)时,可以指定非关键属性的类型,并且不能指定关键属性的类型
  • 使用客户端(另一个选项)时,必须指定所有属性的类型。

使用DynamoDB资源:

resource = boto3.resource('dynamodb', endpoint_url='http://localhost:8000')

mytable = resource.create_table(
    TableName='mytable',
    KeySchema=[{ 'AttributeName': 'name', 'KeyType': 'HASH' }],
    AttributeDefinitions=[{ 'AttributeName': 'name', 'AttributeType': 'S' }],
    ProvisionedThroughput={ 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 }
)

try:
    resource.batch_write_item(RequestItems={
        'mytable': [{ 'PutRequest': { 'Item': {
            'name': { 'S': 'myname' },
            'value': { 'S': 'myvalue' }
        }}}]
    })
    print(f'resource, specify all types : write succeeded.')
except Exception as e:
    print(f'resource, specify all types : write failed: {e}')

try:
    resource.batch_write_item(RequestItems={
        'mytable': [{ 'PutRequest': { 'Item': {
            'name': 'myname',
            'value': { 'S': 'myvalue' }
        }}}]
    })
    print(f'resource, specify value only: write succeeded.')
except Exception as e:
    print(f'resource, specify value only: write failed: {e}')

try:
    resource.batch_write_item(RequestItems={
        'mytable': [{ 'PutRequest': { 'Item': {
            'name': 'myname',
            'value': 'myvalue'
        }}}]
    })
    print(f'resource, specify none      : write succeeded.')
except Exception as e:
    print(f'resource, specify none      : write failed: {e}')

输出量

resource, specify all types : write failed:
    An error occurred (ValidationException) when calling the BatchWriteItem operation: Invalid attribute value type
resource, specify value only: write succeeded.
resource, specify none      : write succeeded.

然后使用DynamoDB客户端(用客户端替换上面的所有“资源”)

client = boto3.client('dynamodb', endpoint_url='http://localhost:8000')
try:
    client.batch_write_item(RequestItems={    
....

输出量

client, specify all types : write succeeded.
client, specify value only: write failed: Parameter validation failed:
    Invalid type for parameter RequestItems.mytable[0].PutRequest.Item.name, value: myname, type: <class 'str'>, valid types: <class 'dict'>
client, specify none      : write failed: Parameter validation failed:
    Invalid type for parameter RequestItems.mytable[0].PutRequest.Item.name, value: myname, type: <class 'str'>, valid types: <class 'dict'>
    Invalid type for parameter RequestItems.mytable[0].PutRequest.Item.value, value: myvalue, type: <class 'str'>, valid types: <class 'dict'>


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/148593/c65ab32aea3490f9fb4a/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

3 0
收藏该文
已收藏

评论内容:(最多支持255个字符)