【问题标题】:"The provided key element does not match the schema" when writing into a table in DynamoDB写入 DynamoDB 中的表时“提供的关键元素与架构不匹配”
【发布时间】:2018-11-09 10:48:15
【问题描述】:

我遵循这些 aws 文档来设置我的 DynamoDB 表:

  1. creating table
  2. loading the data into the table

最初我没有任何顾虑地创建了表格,表格结构如下所示:

名称 > 字符串 (主键

类别 > 字符串

线程 > 数量

消息 > 号码

观看次数 > 数量

当我尝试加载上面第二个超链接中给出的数据时,它会抛出一个异常:

调用时发生错误(ValidationException) BatchWriteItem 操作:提供的关键元素与 架构

我确实通过aws cli 使用了以下命令:

aws dynamodb batch-write-item --request-items file:///home/kula/Documents/aws/sampledata/Forum.json 

这是我尝试加载的json file,我从aws复制了它。

我还查看了这个ticket,并删除了数字的引号,但仍然没有任何运气。我哪里错了?

任何帮助将不胜感激。

【问题讨论】:

  • 你能从你的文件中显示一些行吗?
  • @EyalCh 我已经用 json 文件更新了问题。
  • 你是如何创建表格的?你能显示创建表命令吗?
  • 我通过aws console > DynamoDB > Create Table创建它
  • 你能打印你的表的屏幕索引吗(从 aws 控制台)

标签: amazon-web-services amazon-dynamodb


【解决方案1】:

我显然最终创建了 python 脚本来创建表并将数据加载到其中。

创建表:

import boto3    

dynamodb = boto3.resource('dynamodb', region_name='us-east-1')    

table = dynamodb.create_table(
    TableName='users',
    KeySchema=[
        {
            'AttributeName': 'username',
            'KeyType': 'HASH'
        },
        {
            'AttributeName': 'last_name',
            'KeyType': 'RANGE'
        }
    ],
    AttributeDefinitions=[
        {
            'AttributeName': 'username',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'last_name',
            'AttributeType': 'S'
        },

    ],
    ProvisionedThroughput={
        'ReadCapacityUnits': 1,
        'WriteCapacityUnits': 1
    }
)

# Wait until the table exists.
table.meta.client.get_waiter('table_exists').wait(TableName='users')

将数据加载到表中:

#!/usr/bin/python
import boto3

dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

table = dynamodb.Table('users')

with table.batch_writer() as batch:
    batch.put_item(
        Item={
            'account_type': 'standard_user',
            'username': 'johndoe',
            'first_name': 'John',
            'last_name': 'Doe',
            'age': 25,
            'address': {
                'road': '1 Jefferson Street',
                'city': 'Los Angeles',
                'state': 'CA',
                'zipcode': 90001
            }
        }
    )
    batch.put_item(
        Item={
            'account_type': 'standard_user',
            'username': 'bobsmith',
            'first_name': 'Bob',
            'last_name':  'Smith',
            'age': 18,
            'address': {
                'road': '3 Madison Lane',
                'city': 'Louisville',
                'state': 'KY',
                'zipcode': 40213
            }
        }
    )
    batch.put_item(
        Item={
            'account_type': 'super_user',
            'username': 'alicedoe',
            'first_name': 'Alice',
            'last_name': 'Doe',
            'age': 27,
            'address': {
                'road': '1 Jefferson Street',
                'city': 'Los Angeles',
                'state': 'CA',
                'zipcode': 90001
            }
        }
    )    

print(table.creation_date_time)

希望这对某人有所帮助!

【讨论】:

    【解决方案2】:

    我在尝试通过 aws dynamodb batch-write-item --request-items file://data/sample_services_data.json 批量插入时遇到了类似的问题

    事实证明,您的主分区键是区分大小写的。在下面的这种情况下,“77”将导致导入失败。如果将其更改为 'id' 而不是 'Id' ,它将导入而不会引发架构错误。

            {
                "PutRequest": {
                    "Item": {
                        "Id": { "S": "77" },
                        "name": {"S":"Amazon Web Services"},
                        "language": {"S":"Python"},
                        "description": {"S":"Awesome super service"}
                    }
                }
            },
            {
                "PutRequest": {
                    "Item": {
                        "id": {"S":"99"},
                        "name": {"S":"Epic Service"},
                        "language": {"S":"NodeJS"},
                        "description": {"S":"Awesome epic service"}
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-26
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多