【Azure 存储服务】Python模块(azure.cosmosdb.table)直接对表存储(Storage Account Table)做操作示例

简介: 【Azure 存储服务】Python模块(azure.cosmosdb.table)直接对表存储(Storage Account Table)做操作示例

什么是表存储

Azure 表存储是一项用于在云中存储结构化 NoSQL 数据的服务,通过无结构化的设计提供键/属性存储。 因为表存储无固定的数据结构要求,因此可以很容易地随着应用程序需求的发展使数据适应存储。Azure 表存储可存储大量结构化数据。 该服务是一个 NoSQL 数据存储,接受来自 Azure 云内部和外部的通过验证的呼叫。 Azure 表最适合存储结构化非关系型数据。 表存储的常见用途包括:

  • 存储 TB 量级的结构化数据,能够为 Web 规模应用程序提供服务
  • 存储无需复杂联接、外键或存储过程,并且可以对其进行非规范化以实现快速访问的数据集
  • 使用聚集索引快速查询数据
  • 使用 OData 协议和 LINQ 查询以及 WCF 数据服务 .NET 库访问数据

可以使用表存储来存储和查询大型结构化非关系型数据集,并且表会随着需求的增加而扩展。表存储包含以下组件:

(内容来源于 Azure: https://docs.azure.cn/zh-cn/cosmos-db/table-storage-overview

 

问题描述

是否有Python module可以直接对Storage Account Table(表存储)进行操作呢? 有的。在查询表存储的Python文档后,它使用的Python module于cosmosDB一样。在代码中引入azure.cosmosdb.table即可。

from azure.cosmosdb.table.tableservice import TableService

from azure.cosmosdb.table.models import Entity

使用PIP安装azure-cosmosdb-table模块。在VS Code中执行: python -m pip install azure-cosmosdb-table

 

操作代码

通过 Python 开始使用 Azure 表存储和 Azure Cosmos DB 表 API

此示例介绍如何在常见的 Azure 表存储方案中使用用于 Python 的 Azure Cosmos DB 表 SDK该 SDK 的名称表示它适合与 Azure Cosmos DB 配合使用,但其实该 SDK 既适合与 Azure Cosmos DB 配合使用,也适合与 Azure 表存储配合使用,只不过每个服务具有唯一的终结点。 本文使用 Python 示例探索这些方案,以演示如何:

  • 创建和删除表
  • 插入和查询实体
  • 修改实体

Source:https://docs.azure.cn/zh-cn/cosmos-db/table-storage-how-to-use-python?toc=https%3A%2F%2Fdocs.azure.cn%2Fzh-cn%2Fstorage%2Ftables%2Ftoc.json&bc=https%3A%2F%2Fdocs.azure.cn%2Fzh-cn%2Fbread%2Ftoc.json

Python cosmosdb table模块中包含了对表的所有原子操作。以下代码示例中包含了:

  • 创建表:create_table
  • 将实体添加到表:insert_entity
  • 更新实体:update_entity / insert_or_replace_entity
  • 修改多个实体: with table_service.batch(tablename) as batch
  • 查询实体: get_entity
  • 查询一组实体: table_service.query_entities(tablename, filter="PartitionKey eq 'tasksSeattle'")
  • 查询一部分实体属性:table_service.query_entities(tablename, filter="PartitionKey eq 'tasksSeattle'", select='description')
  • 删除实体:delete_entity
  • 删除表:delete_table

全部代码:

from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity
##连接到 Azure 表服务, account_key的内容在Azure Storage Account的门户中获取(Storage Account --> Access Keys)
table_service = TableService(account_name='you storage account name', account_key='your storage account key', endpoint_suffix='core.chinacloudapi.cn')
##创建表
tablename='tasktable2'
table_service.create_table(tablename)
##将实体添加到表
task = {'PartitionKey': 'tasksSeattle', 'RowKey': '001',
        'description': 'Take out the trash', 'priority': 200}
table_service.insert_entity(tablename, task)
## Same way:
# task = Entity()
# task.PartitionKey = 'tasksSeattle'
# task.RowKey = '002'
# task.description = 'Wash the car'
# task.priority = 100
# table_service.insert_entity(tablename, task)
##更新实体
print('##更新实体')
task = {'PartitionKey': 'tasksSeattle', 'RowKey': '001',
        'description': 'Take out the garbage', 'priority': 250}
table_service.update_entity(tablename, task)
##如果要更新的实体不存在,更新操作将失败。 如果要存储实体(无论其存在与否)
print('##如果要更新的实体不存在,更新操作将失败。 如果要存储实体(无论其存在与否)')
# Replace the entity created earlier
task = {'PartitionKey': 'tasksSeattle', 'RowKey': '001',
        'description': 'Take out the garbage again', 'priority': 250}
table_service.insert_or_replace_entity(tablename, task)
# Insert a new entity
task = {'PartitionKey': 'tasksSeattle', 'RowKey': '003',
        'description': 'Buy detergent', 'priority': 300}
table_service.insert_or_replace_entity(tablename, task)
##修改多个实体
print('##修改多个实体')
task006 = {'PartitionKey': 'tasksSeattle', 'RowKey': '006',
           'description': 'Go grocery shopping', 'priority': 400}
task007 = {'PartitionKey': 'tasksSeattle', 'RowKey': '007',"MyAddColumn":"you know, thing changed, life goes on.",
           'description': 'Clean the bathroom', 'priority': 100}
with table_service.batch(tablename) as batch:
    batch.insert_entity(task006)
    batch.insert_entity(task007)
##查询条目/实体
print('##查询条目/实体')
task = table_service.get_entity(tablename, 'tasksSeattle', '001')
print(task.description)
print(task.priority)
##查询一组条目
print('##查询一组条目')
tasks = table_service.query_entities(
    tablename, filter="PartitionKey eq 'tasksSeattle'")
for task in tasks:
    print(task.description)
    print(task.priority)
##查询一部分实体属性
print('##查询一部分实体属性')
tasks = table_service.query_entities(
    tablename, filter="PartitionKey eq 'tasksSeattle'", select='description')
for task in tasks:
    print(task.description)
# ##删除条目/实体
# print('##删除条目/实体')
# table_service.delete_entity(tablename, 'tasksSeattle', '001')
# ##删除表
# print('##删除表')
# table_service.delete_table(tablename)

执行结果:

 

参考文档

什么是 Azure 表存储?:https://docs.azure.cn/zh-cn/storage/tables/table-storage-overview

通过 Python 开始使用 Azure 表存储和 Azure Cosmos DB 表 API: https://docs.azure.cn/zh-cn/cosmos-db/table-storage-how-to-use-python?toc=https%3A%2F%2Fdocs.azure.cn%2Fzh-cn%2Fstorage%2Ftables%2Ftoc.json&bc=https%3A%2F%2Fdocs.azure.cn%2Fzh-cn%2Fbread%2Ftoc.json

TableService Class: https://docs.microsoft.com/en-us/python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice?preserve-view=true&view=azure-python#insert-entity-table-name--entity--timeout-none-

相关文章
|
4天前
|
机器学习/深度学习 数据采集 算法
数据稀缺条件下的时间序列微分:符号回归(Symbolic Regression)方法介绍与Python示例
有多种方法可以处理时间序列数据中的噪声。本文将介绍一种在我们的研究项目中表现良好的方法,特别适用于时间序列概况中数据点较少的情况。
19 1
数据稀缺条件下的时间序列微分:符号回归(Symbolic Regression)方法介绍与Python示例
|
3天前
|
Kubernetes API 开发工具
【Azure Developer】通过SDK(for python)获取Azure服务生命周期信息
需要通过Python SDK获取Azure服务的一些通知信息,如:K8S版本需要更新到指定的版本,Azure服务的维护通知,服务处于不健康状态时的通知,及相关的操作建议等内容。
36 18
|
10天前
|
Java Serverless Python
探索Python中的并发编程与`concurrent.futures`模块
探索Python中的并发编程与`concurrent.futures`模块
14 4
|
21天前
|
Python
如何在 Python 中导入模块
【8月更文挑战第29天】
20 1
|
21天前
|
Python
|
21天前
|
数据采集 JSON 算法框架/工具
我常用的几个经典Python模块
我常用的几个经典Python模块
|
3天前
|
数据处理 开发者 Python
探索Python中的列表推导式在Python编程中,列表推导式是一种简洁而高效的方法,用于从现有的列表创建新列表。本文将深入探讨列表推导式的用法、优势以及一些实际应用示例。
列表推导式是Python提供的一种强大工具,它允许开发者以更简洁的语法快速生成列表。通过结合循环和条件语句,列表推导式能够简化代码结构,提高开发效率。本文详细介绍了列表推导式的基本用法,并通过实例展示了其在数据处理、转换和过滤中的广泛应用。
9 0
|
20天前
|
数据采集 存储 JavaScript
构建你的首个Python网络爬虫:抓取、解析与存储数据
【8月更文挑战第31天】在数字时代的浪潮中,数据成为了新的石油。了解如何从互联网的海洋中提取有价值的信息,是每个技术爱好者的必备技能。本文将引导你通过Python编程语言,利用其强大的库支持,一步步构建出你自己的网络爬虫。我们将探索网页请求、内容解析和数据存储等关键环节,并附上代码示例,让你轻松入门网络数据采集的世界。
|
21天前
|
开发者 Python
什么是 python 模块?
【8月更文挑战第29天】
11 0
|
4月前
|
Python 人工智能 数据可视化
Python模块与包(八)
Python模块与包(八)
41 0
Python模块与包(八)