[azure storage service] Python module( azure.cosmosdb.table )Direct operation example of storage account table

azure storage service python module


What is table storage

Azure Table storage is a way to store structured data in the cloud NoSQL Data services , Provide keys through unstructured design / Property store .  Because table storage has no fixed data structure requirements , So it's easy to adapt data to storage as application requirements evolve .Azure Table storage can store large amounts of structured data .  The service is a NoSQL data storage , from Azure Verified calls inside and outside the cloud . Azure Tables are best for storing structured, non relational data .  Common uses of table storage include :

  • Storage TB Structured data of the order of magnitude , Can be Web Scale applications provide services
  • Storage does not require complex joins 、 Foreign keys or stored procedures , And it can be denormalized to achieve fast access to data sets
  • Use clustered indexes to quickly query data
  • Use OData The protocol and LINQ Inquiry and WCF Data services .NET Library access data

Table storage can be used to store and query large structured non relational datasets , And the table will expand as the demand increases . The table store contains the following components :

( The content comes from Azure: https://docs.azure.cn/zh-cn/cosmos-db/table-storage-overview

 

Problem description

Is there a Python module It can be directly to Storage Account Table( Table is stored ) To operate ? yes , we have . Stored in the query table Python After the documents , It USES Python module On cosmosDB equally . Introduce... Into your code azure.cosmosdb.table that will do .

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

Use PIP install azure-cosmosdb-table modular . stay VS Code In the implementation of : python -m pip install azure-cosmosdb-table

 

Operation code

adopt Python Start using Azure Table storage and Azure Cosmos DB surface API

This example shows how to use the common Azure Table storage scheme be used for Python Of Azure Cosmos DB surface SDK The SDK The name of indicates that it is suitable for Azure Cosmos DB In combination with , But it's time to SDK It is suitable for Azure Cosmos DB In combination with , It's also suitable for Azure Table storage is used with , It's just that each service has a unique endpoint .  This article USES the Python Examples explore these solutions , To demonstrate how to :

  • Create and delete tables
  • Insert and query entities
  • Modify the entity

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 The module contains all the atomic operations on the table . The following code example contains :

  • Create table :create_table
  • Add entity to table :insert_entity
  • Update entity :update_entity / insert_or_replace_entity
  • Modify multiple entities : with table_service.batch(tablename) as batch
  • Query entities : get_entity
  • Query a set of entities : table_service.query_entities(tablename, filter="PartitionKey eq 'tasksSeattle'")
  • Query part of the entity properties :table_service.query_entities(tablename, filter="PartitionKey eq 'tasksSeattle'", select='description')
  • Delete entities :delete_entity
  • Delete table :delete_table

All the code :

from azure.cosmosdb.table.tableservice import TableService
from azure.cosmosdb.table.models import Entity
## Connect to Azure Table service , account_key The content is in Azure Storage Account From the portal of (Storage Account --> Access Keys)
table_service = TableService(account_name='you storage account name', account_key='your storage account key', endpoint_suffix='core.chinacloudapi.cn')
## Create table 
tablename='tasktable2'
table_service.create_table(tablename)
## Add entity to table 
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)
## Update entity 
print('## Update entity ')
task = {'PartitionKey': 'tasksSeattle', 'RowKey': '001',
'description': 'Take out the garbage', 'priority': 250}
table_service.update_entity(tablename, task)
## If the entity to be updated does not exist , The update operation will fail . If you want to store entities ( Whether it exists or not )
print('## If the entity to be updated does not exist , The update operation will fail . If you want to store entities ( Whether it exists or not )')
# 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)
## Modify multiple entities 
print('## Modify multiple entities ')
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)
## Query entry / Entity 
print('## Query entry / Entity ')
task = table_service.get_entity(tablename, 'tasksSeattle', '001')
print(task.description)
print(task.priority)
## Query a set of items 
print('## Query a set of items ')
tasks = table_service.query_entities(
tablename, filter="PartitionKey eq 'tasksSeattle'")
for task in tasks:
print(task.description)
print(task.priority)
## Query part of the entity properties 
print('## Query part of the entity properties ')
tasks = table_service.query_entities(
tablename, filter="PartitionKey eq 'tasksSeattle'", select='description')
for task in tasks:
print(task.description)
# ## Delete entry / Entity 
# print('## Delete entry / Entity ')
# table_service.delete_entity(tablename, 'tasksSeattle', '001')
# ## Delete table 
# print('## Delete table ')
# table_service.delete_table(tablename)

Execution results :

 

Reference documents

What is? Azure Table is stored ?:https://docs.azure.cn/zh-cn/storage/tables/table-storage-overview

adopt Python Start using Azure Table storage and Azure Cosmos DB surface 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-

 

版权声明
本文为[Two lights on the side of the road]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/01/20210121160112146w.html

  1. LeetCode | 0508. 出现次数最多的子树元素和【Python】
  2. Leetcode | 0508
  3. LeetCode | 0530. 二叉搜索树的最小绝对差【Python】
  4. LeetCode | 0515. 在每个树行中找最大值【Python】
  5. Leetcode | 0530. Minimum absolute difference of binary search tree [Python]
  6. Leetcode | 0515. Find the maximum value in each tree row [Python]
  7. 我来记笔记啦-搭建python虚拟环境
  8. Let me take notes - building a python virtual environment
  9. LeetCode | 0513. 找树左下角的值【Python】
  10. Leetcode | 0513. Find the value in the lower left corner of the tree [Python]
  11. Python OpenCV 泛洪填充,取经之旅第 21 天
  12. Python opencv flood fill, day 21
  13. Python爬虫自学系列(二)
  14. Python crawler self study series (2)
  15. 【python】身份证号码有效性检验
  16. [Python] validity test of ID number
  17. Python ORM - pymysql&sqlalchemy
  18. Python ORM - pymysql&sqlalchemy
  19. centos7 安装python3.8
  20. centos7 安装python3.8
  21. Centos7 installing Python 3.8
  22. Centos7 installing Python 3.8
  23. Django——图书管理系统(六)
  24. Django——图书管理系统(五)
  25. Django -- library management system (6)
  26. Django -- library management system (5)
  27. python批量插入数据小脚本
  28. Python batch insert data script
  29. ZoomEye-python 使用指南
  30. Zoomeye Python User's Guide
  31. 用Python写代码,一分钟搞定一天工作量,同事直呼:好家伙 - 知乎
  32. Using Python to write code, one minute to complete a day's workload, colleagues call: good guy - Zhihu
  33. Python 上的可视化库——PyG2Plot
  34. Pyg2plot: a visualization library on Python
  35. Python 上的可视化库——PyG2Plot
  36. Python实用代码-无限级分类树状结构生成算法
  37. Pyg2plot: a visualization library on Python
  38. Python utility code - infinite classification tree structure generation algorithm
  39. 奇技淫巧,还是正统功夫?Python推导式最全用法
  40. Pandas 的这个知识点,估计 80% 的人都得挂!
  41. 前后端分离有什么了不起,手把手教你用Python爬下来!
  42. 在 Azure 上执行一些简单的 python 工作
  43. 推荐 :利用Python的混合集成机器学习(附链接)
  44. Cunning or orthodox Kung Fu? The most complete usage of Python derivation
  45. It's estimated that 80% of pandas people have to hang up!
  46. What's so great about the separation of front and rear ends? Hand in hand teach you to climb down with Python!
  47. Doing some simple Python work on azure
  48. Recommendation: hybrid integrated machine learning using python (link attached)
  49. Learning PPO algorithm programming from scratch (Python version)
  50. Python OpenCV 图片模糊操作 blur 与 medianBlur
  51. Python OpenCV image blur operation blur and mediablur
  52. 成功解决cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: err
  53. Cv2.error solved successfully: opencv (4.1.2) C:: (projects / opencv Python / opencv modules / imgproc / SRC)\ color.cpp:182 : err
  54. Python 中使用 virtualenv 管理虚拟环境
  55. Using virtualenv to manage virtual environment in Python
  56. 如何使用Python执行系统命令?Python学习教程!
  57. How to use Python to execute system commands? Python tutorial!
  58. 快速掌握Python中的循环技术
  59. Quickly grasp the loop technology in Python
  60. Python主流Web框架之Tornado