Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"The provided key element does not match the schema" error when getting an item from DynamoDB

This is the table partition key setting enter image description here

The table content enter image description here

When I tried to get an item from the table, it prints this error

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema

This is my code

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('testDynamodb')
response = table.get_item(Key={'userId': "user2873"})
item = response['Item']
print(item)

Any ideas? thanks.

like image 337
Keoros Avatar asked Mar 13 '17 06:03

Keoros


People also ask

Which statement retrieves an item from the music collection table in DynamoDB?

In SQL, you use the SELECT statement to retrieve data from a table. You can request one or more columns in the result (or all of them, if you use the * operator). The WHERE clause determines which rows to return. The following is a SELECT statement to retrieve a single row from the Music table.

What is range key in DynamoDB?

The sort key of an item is also known as its range attribute. The term range attribute derives from the way DynamoDB stores items with the same partition key physically close together, in sorted order by the sort key value. Each primary key attribute must be a scalar (meaning that it can hold only a single value).

How do I remove the sort key in DynamoDB?

A DynamoDB table's Sort Key and Partition Key definitions cannot be changed. (You can update an individual record's PK and SK values, but you cannot change the PK or SK setting.) It is a typical pattern to use the generic key names PK and SK , for this reason (and that the pattern also enables a single-table design).

Which one of the following data types does Amazon DynamoDB not support?

Unlike conventional relational databases, DynamoDB does not natively support a date and time data type.


2 Answers

Your table schema has both hash key and sort key defined. When using DynamoDB GetItem you must provide both of them, here is an excerpt from documentation

For the primary key, you must provide all of the attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide values for both the partition key and the sort key.

So given your example, here is how get_item parameters should look like:

response = table.get_item(Key={'userId': "user2873", 'createdAt': "1489376547"})
like image 135
xtx Avatar answered Oct 16 '22 17:10

xtx


One other thing that works is the following code below:

from boto3.dynamodb.conditions import Key

result = table.query(
        KeyConditionExpression=Key('userId').eq('user2873')
    )
like image 29
user754036 Avatar answered Oct 16 '22 18:10

user754036