Azure table 存储 - 使用 next_row_key returns 相同的块
Azure table storage - using next_row_key returns the same chunk
我正在尝试使用 query_entities 响应中的延续令牌来检索接下来的 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 中的解释来看,它似乎应该有效。
这是解决该问题的解决方法。由于我无法使用延续令牌来获取下一行键,我正在使用最后一行键手动更新查询,并且我仅将延续令牌用作终止条件。
这是一个范围查询的例子。假设我们要检索行键 123456789
和 123457689
之间的所有值,这些值的范围大于 1000 行。
rows = table_service.query_entities(
'tableName', ("PartitionKey eq '6' Rowkey gt '123456789' and "
"Rowkey lt '123457689'"), top=1000)
data = rows
while hasattr(rows, 'x_ms_continuation'):
# now using the last retrieved rowkey inside the query
rows = table_service.query_entities(
'tableName', ("PartitionKey eq '6' Rowkey gt '" +
rows[len(rows)-1].RowKey + "' and "
"Rowkey lt '123457689'"), top=1000)
data.extend(rows)
当 table 服务 returns 延续令牌时,您会得到两件事 - NextPartitionKey
和 NextRowKey
。要获取下一组实体,您需要同时使用它们。请执行以下操作:
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
如果您使用最新版本的 azure-storage-table
(隐藏在 azure-cosmosdb-table
pip 包中),您可以免费获得超过 1000 个长块的迭代。 top
query_entities
的参数甚至不再可用。
此代码段适用于 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
我正在尝试使用 query_entities 响应中的延续令牌来检索接下来的 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 中的解释来看,它似乎应该有效。
这是解决该问题的解决方法。由于我无法使用延续令牌来获取下一行键,我正在使用最后一行键手动更新查询,并且我仅将延续令牌用作终止条件。
这是一个范围查询的例子。假设我们要检索行键 123456789
和 123457689
之间的所有值,这些值的范围大于 1000 行。
rows = table_service.query_entities(
'tableName', ("PartitionKey eq '6' Rowkey gt '123456789' and "
"Rowkey lt '123457689'"), top=1000)
data = rows
while hasattr(rows, 'x_ms_continuation'):
# now using the last retrieved rowkey inside the query
rows = table_service.query_entities(
'tableName', ("PartitionKey eq '6' Rowkey gt '" +
rows[len(rows)-1].RowKey + "' and "
"Rowkey lt '123457689'"), top=1000)
data.extend(rows)
当 table 服务 returns 延续令牌时,您会得到两件事 - NextPartitionKey
和 NextRowKey
。要获取下一组实体,您需要同时使用它们。请执行以下操作:
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
如果您使用最新版本的 azure-storage-table
(隐藏在 azure-cosmosdb-table
pip 包中),您可以免费获得超过 1000 个长块的迭代。 top
query_entities
的参数甚至不再可用。
此代码段适用于 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