python查询所有azure表行

时间:2015-01-19 07:00:38

标签: python azure azure-table-storage

我的天蓝色表中有大约20000行。我想查询azure表中的所有行。但是由于某些天蓝色的限制,我只能获得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

我想从validoutputtable获取所有行。是否有办法这样做

3 个答案:

答案 0 :(得分:7)

  

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

这是一个记录在案的限制。对Azure Table的每个查询请求将返回不超过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;

答案 1 :(得分:0)

更新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
...

答案 2 :(得分:0)

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

pip install azure-data-tables

要使用最新的库查询给定表的所有行,您可以使用以下代码片段:

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
...