python查询所有azure表行

4 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
Run Code Online (Sandbox Code Playgroud)

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

Gau*_*tri 8

但由于某些天蓝色的限制,我只得到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;
Run Code Online (Sandbox Code Playgroud)


小智 6

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

pip install azure-data-tables
Run Code Online (Sandbox Code Playgroud)

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

pip install azure-data-tables
Run Code Online (Sandbox Code Playgroud)

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

...
1100
1200
1300
...
Run Code Online (Sandbox Code Playgroud)


Ole*_*mov 5

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 
Run Code Online (Sandbox Code Playgroud)

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

...
Processing 363500 record
Processing 363600 record
...
Run Code Online (Sandbox Code Playgroud)