python 查询 azure 的所有行 table

python querying all rows of azure table

我的 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

我想从有效输出中获取所有行table。有没有办法做到这一点

But due to certain azure limitation i am getting only 1000 rows.

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

请参阅示例代码以从 table:

中获取所有实体
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;

2019 年更新

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

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

A​​zure Table Storage 在预览版中有一个新的 python 库,可通过 pip 安装。要安装,请使用以下 pip 命令

pip install azure-data-tables

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

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