python - 如何使用 python sdk 访问 Azure 存储表中的最新条目?

标签 python azure azure-table-storage azure-storage-account azure-tablequery

我正在使用以下代码:

from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity

table_service = TableService(account_name='', account_key='')

task = table_service.get_entity('table1', '71cb50aa-75ac-4363-a14e-26df95c9b418', 'SCHEDULE')
print(task)

task2 = table_service.query_entities('table1', filter=None, select=None, num_results=1, marker=None, accept='application/json;odata=minimalmetadata', property_resolver=None, timeout=None)

print(task2)

但是在编写Python代码时,我不知道分区键。 我想检查表“table1”中的最新条目是否包含“SCHEDULE”。

最佳答案

听起来你想知道如何使用函数 query_entities(table_name, filter=None, select=None, num_results=None, marker=None, accept='application/json;odata=minimalmetadata', property_resolver=None, timeout=None)如下图列出表 table1 的最新实体,其中 SCHEDULE RowKey 值。

enter image description here

据我所知,需要通过filter参数来实现,如下代码。

filter = "RowKey eq 'SCHEDULE'
task2 = table_service.query_entities('table1', filter=filter)
print(list(task2))

上面的代码会列出所有RowKey SCHEDULEPartitionKey未知的实体,请引用官方文档Querying Tables and Entities了解有关使用 filter 参数的更多详细信息。

但是由于不支持按Timestamp对实体进行排序,因此无法直接通过filternum_results=1<来获取最新的实体。如果有一种模式可以知道最新实体的时间戳范围,您可以尝试使用下面的代码。

import datetime
from operator import itemgetter

filter = "RowKey eq 'SCHEDULE' and Timestamp lt datetime'{}'".format(datetime.datetime.utcnow().isoformat()) # such as `2019-07-29T07:41:40.897643`
# Or filter = "RowKey eq 'SCHEDULE' and Timestamp ge datetime'<the start of datetime range>' and Timestamp lt datetime'<the end of datetime range>'"
task2 = table_service.query_entities('table1', filter=filter)
    print(list(task2))
newlist = sorted(task2, key=itemgetter('Timestamp'))
latest_entity = newlist[0]
print(latest_entity)

如果你想要一个简单的解决方案,我认为带有表存储触发器的Azure Function会有所帮助,请引用官方文档Azure Table storage bindings for Azure Functions支持Python。

希望有帮助。

关于python - 如何使用 python sdk 访问 Azure 存储表中的最新条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57218840/

相关文章:

python - 如何增加matplotlib中axis_artist的箭头大小

powershell - 在 Azure 云服务上将防恶意软件设置为默认启用

azure - TFS 构建服务生成一个包,其中引用了错误版本的 msshrtmi(应该是 1.7,最终引用了 1.8)

python azure存储实体对象

azure-storage - 写入 azure 表存储时,我们有时会看到行为

python - 什么是 Python 中的顶级语句?

java - Java 和 Python 之间的一年计算差异

python - 如何使用numba加速快速排序?

Azure SQL DB 分布计数

java - 检索 Azure 表中某个分区的所有实体