使用python从表存储中检索1000多行

使用python从表存储中检索1000多行,python,azure-storage,Python,Azure Storage,我使用本论坛中的以下模式从表存储中提取数据: next_pk=0 next_rk=0 while True: xa=table_service.query_entities(table_name=tableName, filter=dataFilter, num_results=1000, next_partition_key = next_pk, next_row_key = next_rk ) i+=1 if hasattr(accelxa, 'x_ms_contin

我使用本论坛中的以下模式从表存储中提取数据:

next_pk=0
next_rk=0
while True:
    xa=table_service.query_entities(table_name=tableName, filter=dataFilter, num_results=1000, next_partition_key = next_pk, next_row_key = next_rk )
    i+=1
    if hasattr(accelxa, 'x_ms_continuation'):
        x_ms_continuation = getattr(accelxa, 'x_ms_continuation')
        next_pk = x_ms_continuation['nextpartitionkey']
        next_rk = x_ms_continuation['nextrowkey']
    else:
        break;
但当我运行它时,我得到以下异常:

TypeError was unhandled by user code
Message: query_entities() got an unexpected keyword argument 'next_partition_key'
因此,api似乎与编写示例时有所不同,我无法确定新api是什么

我看过github,在方法调用中有对标记对象的引用,但我不清楚如何使用它。任何例子都会有帮助。

看看Github代码:

def _query_entities(self, table_name, filter=None, select=None, max_results=None,
                   marker=None, accept=TablePayloadFormat.JSON_MINIMAL_METADATA,
                   property_resolver=None, timeout=None, _context=None):
        ...

        next_partition_key = None if marker is None else marker.get('nextpartitionkey')
根据该语法,您必须将
标记
关键字参数初始化为字典(
dict
),并将
标记['nextpartitionkey']
(而不是
'next\u partition\u key'
)设置为所需的值

xa = table_service.query_entities(..., marker={'nextpartitionkey'= ...}, ...)

您还应该从代码中删除
next_row_key
关键字参数,并以相同的方式初始化
marker['nextrowkey']

我试图成功重现该问题,然后我发现问题是由代码中的
query\u entities
方法引起的,该方法与您安装的
azure storage
软件包版本不兼容

根据GitHub的源代码,您可以找到如下版本差异

在版本
v0.20.x
中,例如,最新版本
v0.20.3
,如下所示,它具有参数
next\u partition\u key
next\u row\u key

但在
v0.20.3
之后,更改为@DmitryFrov所说的,如下所示

因此,有两种解决方案

  • 通过命令
    pip uninstall azure storage
    &
    pip install azure storage==0.20.3
    回滚软件包版本,然后代码将正常工作
  • 正如@DmitryFrolov所说,在当前版本下,构造一个
    标记
    对象,以包含参数
    next\u partition\u key
    &
    next\u row\u key
    ,以遵循新用法

  • 使用azure存储0.34.3,我可以使用以下代码:

    table_service = TableService(account_name, account_key)
    i=0
    marker = None
    while True:
        tasks = table_service.query_entities(table_name, data_filter,
                                             marker = marker,
                                             num_results=1000)
        for task in tasks:
          i += 1
          print(task)
        if tasks.next_marker is not None and len(tasks.next_marker) > 0:
          marker = tasks.next_marker
        else:
            break
    
    
    print i
    
    请注意,您不再需要构造自己的标记对象。只需使用返回给您的标记词典,并检查标记词典是否确实包含一些内容;如果有更多行要处理,则标记字典应该有两个项

    def query_entities(self, table_name, filter=None, select=None, top=None, 
                       next_partition_key=None, next_row_key=None):
    
    def query_entities(self, table_name, filter=None, select=None, num_results=None,
                       marker=None, accept=TablePayloadFormat.JSON_MINIMAL_METADATA,
                       property_resolver=None, timeout=None):
    
    table_service = TableService(account_name, account_key)
    i=0
    marker = None
    while True:
        tasks = table_service.query_entities(table_name, data_filter,
                                             marker = marker,
                                             num_results=1000)
        for task in tasks:
          i += 1
          print(task)
        if tasks.next_marker is not None and len(tasks.next_marker) > 0:
          marker = tasks.next_marker
        else:
            break
    
    
    print i