Anthony Williams
Anthony Williams

Reputation: 83

"Key element does not match the schema" for only one Partition Key with No Sort Keys and No Indexes

I have a problem with a DynamoDB table. I only have the partition key with no index and no sort key, yet I still get this Client error when adding rows of data into the Dynamo table. Since I only have one partition key, I am confused as to why I am receiving this error because it looks like the key element (uniqueKey) does match the schema.

Here is the code (I shortened this for brevity, but I'm simply reading rows of CSV file data from a FOR loop and loading the data into a DynamoTable):

if 'UNIQUE_KEY' in each and each['UNIQUE_KEY'] != 'null' and each['UNIQUE_KEY'] is not None and each['UNIQUE_KEY'] != '':
        dbObj['uniqueKey'] = str(each['UNIQUE_KEY'])
        print('<<uniqueKey>> ', dbObj['uniqueKey']) 
         try:
            response = table.get_item(
            Key={
                'uniqueKey': dbObj['uniqueKey']
                        }
            )
            print(response)
       except ClientError as e:
          print('error message')
          print(e.response['Error']['Message'])`

And here is my table information:

enter image description here

Finally: Here is the echo output for the unique Key called uniqueKey in the lambda's Cloud Watch debug that I used to show that the Partition key is getting a value:

enter image description here

And here is the instantiation of the tables: enter image description here

Here are the table attributes: enter image description here

Upvotes: 0

Views: 145

Answers (3)

Anthony Williams
Anthony Williams

Reputation: 83

Thanks to Lee Hannigan who had me rethink the fact that I may have created the partition Key with a space just after the last character. Once I fixed this , my code worked mostly as expected in that I no longer received the error: "Key element does not match the schema" I now get a few errors on some of the input data but more than half of the CSV file data is reading into the Dynamo Table.

Upvotes: 0

Charles
Charles

Reputation: 23823

First of all is this python & boto3? You should mention that and/or include the appropriate tags.

I don't see anything wrong with your get_item(), I'd suggest double checking the table name value you've stored in the env variable.

Personally, unless you've got multiple tables with the same structure but a different name, there seems little reason to store the table name in an environment variable.

Lastly, if you're "loading data" and using get_item() to check for existence before hand so that you only add new items, you can do that with put_item by itself as noted in the docs.

Note

To prevent a new item from replacing an existing item, use a conditional expression that contains the attribute_not_exists function with the name of the attribute being used as the partition key for the table. Since every record must contain that attribute, the attribute_not_exists function will only succeed if no matching item exists.

Upvotes: -1

stellar
stellar

Reputation: 33

You should use put_item API for adding data in dynamoDB instead of get_item.

Upvotes: -1

Related Questions