0
votes

I have been trying to add items to a DynamoDB table using boto, but somehow it doesn't seem to work. I tried using users.Item() and users.put_item but nothing worked. Below is the script that I have in use. import boto.dynamodb2 import boto.dynamodb2.items import json from boto.dynamodb2.fields import HashKey, RangeKey, GlobalAllIndex from boto.dynamodb2.layer1 import DynamoDBConnection from boto.dynamodb2.table import Table from boto.dynamodb2.items import Item from boto.dynamodb2.types import NUMBER

region = "us-east-1"
con = boto.dynamodb2.connect_to_region(region)
gettables = con.list_tables()
mytable = "my_table"

if mytable not in gettables['TableNames']:
    print "The table *%s* is not in the list of tables created. A new table will be created." % req_table
    Table.create(req_table,
              schema = [HashKey('username'),
                        RangeKey('ID', data_type = NUMBER)],
             throughput = {'read': 1, 'write': 1})
else:    
    print "The table *%s* exists." % req_table

con2table = Table(req_table,connection=con)
con2table.put_item(data={'username': 'abcd',
                         'ID': '001',
                         'logins':'10',
                         'timeouts':'20'
                         'daysabsent': '30'
                         })

I tried this, the table gets created and it is fine. But when I try to put in the items, I get the following error message.

Traceback (most recent call last):
  File "/home/ec2-user/DynamoDB_script.py", line 29, in <module>
    'daysabsent':'30'
  File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/table.py", line 821,       in put_item
    return item.save(overwrite=overwrite)
  File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/items.py", line 455,  in save
    returned = self.table._put_item(final_data, expects=expects)
  File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/table.py", line 835, in _put_item
    self.connection.put_item(self.table_name, item_data, **kwargs)
  File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 1510, in put_item
    body=json.dumps(params))
  File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 2842, in make_request
    retry_handler=self._retry_handler)
  File "/usr/lib/python2.7/dist-packages/boto/connection.py", line 954, in _mexe
    status = retry_handler(response, i, next_sleep)
  File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 2882, in _retry_handler
    response.status, response.reason, data)
boto.dynamodb2.exceptions.ValidationException: ValidationException: 400 Bad Request
{u'message': u'One or more parameter values were invalid: Type mismatch for  key version expected: N actual: S', u'__type':   u'com.amazon.coral.validate#ValidationException'}

Thank you.

2

2 Answers

0
votes

From the error message you are getting, it sounds like you are trying to send string values for an attribute that is defined as numeric in DynamoDB.

The specific issue looks to be related to your Range Key ID which is defined as a numeric value N but you are sending it a string value '001'.

0
votes

Looks like of of the values you are trying to load has empty value. I got the same error when I was trying to load this. I got exception when partner_name property was a empty string.

try:
  item_old = self.table.get_item(hash_key=term)
except BotoClientError as ex:
   # if partner alias does not exist then create a new entry!
     if ex.message == "Key does not exist.":
          item_old = self.table.new_item(term)
            else:
                raise ex
        item_old['partner_code'] = partner_code
        item_old['partner_name'] = partner_name
        item_old.put()