【问题标题】:Code breakes while reading resultset returned by DyanmoDb's table.scan() in python在 python 中读取 DynamoDb table.scan() 返回的结果集时出现代码中断
【发布时间】:2017-06-17 18:01:56
【问题描述】:

我是 dynamoDB 的新手,所以问题可能很愚蠢。 我的目的和代码很简单:

  1. 我连接到 DynamoDB。
  2. 我从 dynamoDB 得到想要的表
  3. 我通过 Table.scan() 读取所有值
  4. 但是当我尝试在第 3 步获得的返回结果集上运行 for 循环而不是通过 resulset 迭代时,我失败并出现以下错误: TypeError: 'ResultSet' 对象没有属性 '__getitem__'

这是我的代码:

     def fireQuery(tableName):
         table = getTable(tableName)
         try :
              result = table.scan(Select="ALL_ATTRIBUTES")
              for item in result['Items'] :  #<-- my code fails here
                  print(item)

     def getTable(tableName):
         try:
              db = getDynamoDBConnection()
              table = Table(tableName, connection=db)        
         except Exception as e:
              print(tableName ," Table doesn't exist.", e)
         return table

控制台异常:

对于用户中的项目['Items']:

TypeError:“ResultSet”对象没有属性“__getitem__”

【问题讨论】:

    标签: python amazon-dynamodb resultset


    【解决方案1】:

    您只需要像这样迭代 ResultSet:

    def fireQuery(tableName):
        table = getTable(tableName)
        try :
             result_set = table.scan(Select="ALL_ATTRIBUTES")
             for item in result_set:
                 print item['attributename']
    

    【讨论】:

    • 我试过你说的,我为 result_object 定义了 getitem 但如果我使用 table.scan(select="ALL_ATTRIBUTES"),我得到这个错误:UnknownFilterTypeError("Operator 'Select' from 'Select' is not Recogniz.",) and for table.scan(),我得到这个错误:JSONResponseError: 400 Bad Request {u'message': u'Requested resource not found', u'__type': u'com.amazonaws.dynamodb.v20120810#ResourceNotFoundException'}
    猜你喜欢
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多