【问题标题】:Adding Item data to a DynamoDB table using boto does not work使用 boto 将项目数据添加到 DynamoDB 表不起作用
【发布时间】:2015-09-28 16:13:19
【问题描述】:

我一直在尝试使用 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 导入表 从 boto.dynamodb2.items 导入项目 从 boto.dynamodb2.types 导入数字

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

谢谢。

【问题讨论】:

    标签: python add amazon-dynamodb boto items


    【解决方案1】:

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

    具体问题似乎与您的范围键 ID 相关,该键被定义为数值 N,但您向其发送的是字符串值 '001'

    【讨论】:

      【解决方案2】:

      看起来您尝试加载的值的值为空。 当我尝试加载它时,我遇到了同样的错误。当 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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-06
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多