0
votes

I want to retrieve single item from DynamoDB but I'm unable to do that. I think I am missing something. I am able to scan the table using scan() function of boto but the fetching of a single item is not working. So it would be great if someone can tell me the correct way to do this.

models.py file:

import os
import boto
from boto import dynamodb2
from boto.dynamodb2.table import Table
from boto.dynamodb2.fields import HashKey, RangeKey, KeysOnlyIndex, GlobalAllIndex


AWS_ACCESS_KEY_ID = '****************'
AWS_SECRET_ACCESS_KEY = '***************************'
REGION = 'us-east-1'
TABLE_NAME = 'Users'

conn = dynamodb2.connect_to_region(REGION, aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

table = Table(
    TABLE_NAME,
    connection=conn
)

results = table.scan()


for dynamo_item in results:
    print dict(dynamo_item.items())

I've tried using following:

results = table.scan(scan_filter={'email':'new'})

but got this error

boto.dynamodb2.exceptions.UnknownFilterTypeError: Operator 'scan_filter' from 'scan_filter' is not recognized.

results = table.query_2(email = 'new')

boto.dynamodb2.exceptions.UnknownFilterTypeError: Operator 'email' from 'email' is not recognized.

results = table.get_item(email='new')

boto.dynamodb2.exceptions.ValidationException: ValidationException: 400 Bad Request {u'message': u'The provided key element does not match the schema', u'__type': u'com.amazon.coral.validate#ValidationException'}

2
what is your table scheme? (hash/range key)Eyal Ch
what is your hash key? (name/type)?Eyal Ch

2 Answers

1
votes

Try this:

results = table.query_2(email__eq = 'new')

You can also use the following:

__eq , __beginswith, __gte

Thanks to

@ing0: dynamodb row count via python, boto query

Source: DynamoDB2

-1
votes

If the table hash key is email then

results = table.get_item(email='new') should work.

Check if email is in type STRING (and not NUMBER) in the table scheme.