使用boto将项目数据添加到DynamoDB表不起作用

问题描述:

我一直在尝试使用boto将项目添加到DynamoDB表中,但不知怎的,它似乎无法工作。我尝试使用users.Item()和users.put_item但没有任何工作。以下是我使用的脚本。 进口boto.dynamodb2 进口boto.dynamodb2.items 进口JSON 从boto.dynamodb2.fields进口HashKey,RangeKey,GlobalAllIndex 从博托boto.dynamodb2.layer1进口DynamoDBConnection 从boto.dynamodb2.table进口表 。 dynamodb2.items进口商品 从boto.dynamodb2.types导入NUMBER使用boto将项目数据添加到DynamoDB表不起作用

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' 
         }) 

我想这一点,该表被创建,它是罚款。但是当我尝试放入项目时,出现以下错误消息。

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'} 

谢谢。

从您收到的错误消息中,听起来您正尝试为在DynamoDB中定义为数字的属性发送字符串值。

具体问题看起来与您的范围键ID有关,该键被定义为数值N,但是您将其发送给字符串值'001'

看起来像你试图加载的值有空值。 当我尝试加载时遇到了同样的错误。当partner_name属性为空字符串时,我收到异常。

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()