Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to get_item from AWS dynamodb using python?

I am new to dynamodb trying to get data from dynamodb.

This is my table with "topic" as a primary hash key

my python code

import boto3 
from boto3 import dynamodb 

from boto3.session import Session

from boto3.dynamodb.conditions import Key, Attr


dynamodb_session = Session(aws_access_key_id='XXXXXXXXXXXXXXX',
          aws_secret_access_key='XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
          region_name='us-east-1')

dynamodb = dynamodb_session.resource('dynamodb')

table=dynamodb.Table('Garbage_collector_table')

my_topic = "$aws/things/garbage_collector_thing/shadow/update/accepted"

response = table.get_item(TableName='Garbage_collector_table', Key={'topic':my_topic})

for res in response: 
    print "result ",res

I am getting the following error

Traceback (most recent call last):
 File "get-data-dynamodb-boto3.py", line 19, in <module>
    response = table.get_item(TableName='Garbage_collector_table', Key={'topic': my_topic})   File
 "/usr/local/lib/python2.7/dist-packages/boto3/resources/factory.py",
 line 518, in do_action
     response = action(self, *args, **kwargs)   File "/usr/local/lib/python2.7/dist-packages/boto3/resources/action.py",
 line 83, in __call__
     response = getattr(parent.meta.client, operation_name)(**params)   File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line
 258, in _api_call
     return self._make_api_call(operation_name, kwargs)   File /usr/local/lib/python2.7/dist-packages/botocore/client.py", line 548,
 in _make_api_call
     raise ClientError(parsed_response, operation_name)

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

am I missing anything in my code?

like image 996
Narendra M Avatar asked Jun 02 '16 10:06

Narendra M


People also ask

What data type is not supported in DynamoDB?

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

How do I access DynamoDB API?

You can access Amazon DynamoDB using the AWS Management Console, the AWS Command Line Interface (AWS CLI), or the DynamoDB API. Javascript is disabled or is unavailable in your browser. To use the Amazon Web Services Documentation, Javascript must be enabled.

Which API is used to write data to DynamoDB?

In Amazon DynamoDB, you can use either the DynamoDB API, or PartiQL, a SQL-compatible query language, to add an item to a table. With the DynamoDB API, you use the PutItem operation to add an item to a table. The primary key for this table consists of Artist and SongTitle. You must specify values for these attributes.


2 Answers

You are mixing resource and client objects which have different methods. More info here.

The correct syntax for a resource is:

response = table.get_item(Key={'topic': my_topic})

but personally I recommend to use boto client:

client = boto3.client('dynamodb')

response = client.get_item(TableName='Garbage_collector_table', Key={'topic':{'S':str(my_topic)}})

http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html

like image 133
Eyal Ch Avatar answered Oct 01 '22 12:10

Eyal Ch


You can also query the database:

from boto3.dynamodb.conditions import Key

table = dynamodb.Table(table_name)
response = table.query(
    KeyConditionExpression=Key('topic').eq(my_topic)
)
items = response['Items']
if items:
    return items[0]
else:
    return []

Source: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.04.html

like image 26
Tomiwa Avatar answered Oct 01 '22 14:10

Tomiwa