使用next_row_key的Azure表存储返回相同的chun

2024-09-27 19:25:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用query_entities响应中的continuation令牌来检索接下来的1000个值,但是我得到了相同的1000个值。在

我的代码

rows = table_service.query_entities(
    'tableName', "PartitionKey eq '6'", top=1000)

nextRowKey = rows.x_ms_continuation['NextRowKey']

rows = table_service.query_entities(
    'tableName', "PartitionKey eq '6'",
    next_row_key=nextRowKey, top=1000)

我做错什么了吗?从github中的解释来看,它似乎应该起作用。在


Tags: 代码topservicetablequerymsrowseq
3条回答

如果您使用azure-storage-table(隐藏在azure-cosmosdb-tablepip包中)的最新版本,您可以免费获得超过1000个长块的迭代。top的参数{}甚至不再可用。在

此片段适用于azure-cosmosdb-table==1.0.5

from azure.cosmosdb.table.tableservice import TableService

service = TableService(connection_string='xxxxx')
table_name = 'yyyy'

rows = service.query_entities(table_name, "PartitionKey lt '2014-07-01'")
for row in rows:
    # do sth with row

当表服务返回一个continuation令牌时,您将得到两个结果-NextPartitionKey和{}。要获取下一组实体,您需要同时使用这两个实体。请执行以下操作:

rows = table_service.query_entities(
    'tableName', "PartitionKey eq '6'", top=1000)

nextRowKey = rows.x_ms_continuation['NextRowKey']
nextPartitionKey = rows.x_ms_continuation['NextPartitionKey']

rows = table_service.query_entities(
    'tableName', "PartitionKey eq '6'", next_partition_key=nextPartitionKey,
    next_row_key=nextRowKey, top=1000)

下面是一个适合我的模式(使用0.33.0的azure存储),我唯一想看到的改进是如何更好地组合结果集。在

def GetData(tableService, tableName, dataFilter):   
    #https://github.com/Azure/azure-storage-python/blob/master/azure/storage/table/tableservice.py
    keyMarkers = {}
    keyMarkers['nextpartitionkey'] = 0
    keyMarkers['nextrowkey'] = 0 
    b=[]
    while True:
        #get a batch of data
        a = tableService.query_entities(table_name=tableName, filter=dataFilter,num_results=1000 ,marker=keyMarkers)
        #copy results to list
        for item in a.items:
            b.append(item)
        #check to see if more data is available
        if len(a.next_marker) == 0:
            del a
            break
        #if more data available setup current position
        keyMarkers['nextpartitionkey'] = a.next_marker['nextpartitionkey']
        keyMarkers['nextrowkey'] = a.next_marker['nextrowkey'] 
        #house keep temp storage
        del a
    #return final list
    return b     

相关问题 更多 >

    热门问题