1

I followed these aws documents in order to set up my DynamoDB table:

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

Initially I created the table without any concern and the table structure would look like this:

Name > String (Primary Key)

Category > String

Threads > Number

Messages > Number

Views > Number

When I tried to load the data, which was given on the 2nd hyperlink above, it throws up an exception saying:

An error occurred (ValidationException) when calling the BatchWriteItem operation: The provided key element does not match the schema

I did use the following command via the aws cli:

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

This is the json file I'm trying to load, which I copied it from aws.

I also had a look into this ticket, and removed the quotes for numbers but still did not have any luck. Where am I going wrong?

Any help would be appreciated.

8
  • can you show some lines from your file?
    – Eyal Ch
    Dec 20, 2017 at 6:04
  • @EyalCh I've updated the question, with the json file.
    – Kulasangar
    Dec 20, 2017 at 7:11
  • how did you create the table? can you show the create table command?
    – Eyal Ch
    Dec 20, 2017 at 7:27
  • I created it through the aws console > DynamoDB > Create Table
    – Kulasangar
    Dec 20, 2017 at 7:29
  • can you print screen indexes of your table (from aws console)
    – Eyal Ch
    Dec 20, 2017 at 7:36

2 Answers 2

0

I apparently ended up creating python scripts to create table and load data into it.

Creating table:

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')

Loading data into the table:

#!/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)

Hope this helps someone!

0

I ran into a similar issue when trying to batch insert via aws dynamodb batch-write-item --request-items file://data/sample_services_data.json

It turns out that your primary partition key is case sensitive. In this scenario below, '77' will fail the import. If you change it to 'id' instead of 'Id' it will import without throwing the schema error.

        {
            "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"}
                }
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.