【问题标题】:python querying all rows of azure tablepython查询天蓝色表的所有行
【发布时间】:2015-01-19 07:00:37
【问题描述】:

我的 azure table 中有大约 20000 行。我想查询 azure table 中的所有行。但由于某些天蓝色的限制,我只能得到 1000 行。

我的代码

from azure.storage import TableService
table_service = TableService(account_name='xxx', account_key='YYY')
i=0
tasks=table_service.query_entities('ValidOutputTable',"PartitionKey eq 'tasksSeattle'")
for task in tasks:
    i+=1
    print task.RowKey,task.DomainUrl,task.Status    
print i

我想从有效输出表中获取所有行。有没有办法这样做

【问题讨论】:

    标签: python azure azure-table-storage


    【解决方案1】:

    但由于某些天蓝色的限制,我只能得到 1000 行。

    这是一个记录在案的限制。对 Azure 表的每个查询请求将返回不超过 1000 行。如果有超过 1000 个实体,表服务将返回一个继续令牌,该令牌必须用于获取下一组实体(请参阅此处的备注部分:http://msdn.microsoft.com/en-us/library/azure/dd179421.aspx

    请查看示例代码以从表中获取所有实体:

    from azure import *
    from azure.storage import TableService
    
    table_service = TableService(account_name='xxx', account_key='yyy')
    i=0
    next_pk = None
    next_rk = None
    while True:
        entities=table_service.query_entities('Address',"PartitionKey eq 'Address'", next_partition_key = next_pk, next_row_key = next_rk, top=1000)
        i+=1
        for entity in entities:
            print(entity.AddressLine1)
        if hasattr(entities, 'x_ms_continuation'):
            x_ms_continuation = getattr(entities, 'x_ms_continuation')
            next_pk = x_ms_continuation['nextpartitionkey']
            next_rk = x_ms_continuation['nextrowkey']
        else:
            break;
    

    【讨论】:

      【解决方案2】:

      2019 年更新

      只需在查询结果上运行 for 循环(正如该主题的作者所做的那样) - 将从查询中获取所有数据。

      from azure.cosmosdb.table.tableservice import TableService
      
      table_service = TableService(account_name='accont_name', account_key='key')
      
      #counter to keep track of records
      counter=0
      
      # get the rows. Debugger shows the object has only 100 records
      rows = table_service.query_entities(table,"PartitionKey eq 'mykey'")
      
      for row in rows:
          if (counter%100 == 0):
              # just to keep output smaller, print every 100 records
              print("Processing {} record".format(counter))
          counter+=1 
      

      输出证明循环超过 1000 条记录

      ...
      Processing 363500 record
      Processing 363600 record
      ...
      

      【讨论】:

        【解决方案3】:

        Azure 表存储在预览版中有一个新的 Python 库,可通过 pip 安装。要安装使用以下 pip 命令

        pip install azure-data-tables
        

        要使用最新库查询给定表的所有行,可以使用以下代码 sn-p:

        from azure.data.tables import TableClient
        
        key = os.environ['TABLES_PRIMARY_STORAGE_ACCOUNT_KEY']
        account_name = os.environ['tables_storage_account_name']
        endpoint = os.environ['TABLES_STORAGE_ENDPOINT_SUFFIX']
        account_url = "{}.table.{}".format(account_name, endpoint)
        table_name = "myBigTable"
        
        with TableClient(account_url=account_url, credential=key, table_name=table_name) as table_client:
        
            try:
                table_client.create_table()
            except:
                pass
        
            i = 0
            for entity in table_client.list_entities():
                print(entity['value'])
                i += 1
                if i % 100 == 0:
                    print(i)
        

        您的前景将如下所示:(为简洁起见进行了修改,假设有 2000 个实体)

        ...
        1100
        1200
        1300
        ...
        

        【讨论】:

        • 应该有一个特别奖来更新最新代码的答案。谢谢!
        猜你喜欢
        • 2012-11-17
        • 1970-01-01
        • 2012-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多