什么是表存儲
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 示例探索這些方案,以演示如何:
- 創建和刪除表
- 插入和查詢實體
- 修改實體
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