Skip to content

Commit dabec29

Browse files
authored
Merge pull request #185231 from AFengKK/upgrade_sdk
Upgrade Table SDK from T1 to T2
2 parents 75756e5 + 19f6f37 commit dabec29

File tree

1 file changed

+73
-98
lines changed

1 file changed

+73
-98
lines changed
Lines changed: 73 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: Use Azure Cosmos DB Table API and Azure Table storage using Python
3-
description: Store structured data in the cloud using Azure Table storage or the Azure Cosmos DB Table API by using Python.
2+
title: Use the Azure Tables client library for Python
3+
description: Store structured data in the cloud using the Azure Tables client library for Python.
44
ms.service: cosmos-db
55
ms.subservice: cosmosdb-table
66
ms.devlang: python
@@ -11,7 +11,7 @@ ms.author: akshanka
1111
ms.reviewer: sngun
1212
ms.custom: devx-track-python
1313
---
14-
# Get started with Azure Table storage and the Azure Cosmos DB Table API using Python
14+
# Get started with Azure Tables client library using Python
1515
[!INCLUDE[appliesto-table-api](../includes/appliesto-table-api.md)]
1616

1717
[!INCLUDE [storage-selector-table-include](../../../includes/storage-selector-table-include.md)]
@@ -23,20 +23,20 @@ You can use the Table storage or the Azure Cosmos DB to store flexible datasets
2323

2424
### About this sample
2525

26-
This sample shows you how to use the [Azure Cosmos DB Table SDK for Python](https://pypi.python.org/pypi/azure-cosmosdb-table/) in common Azure Table storage scenarios. The name of the SDK indicates it is for use with Azure Cosmos DB, but it works with both Azure Cosmos DB and Azure Tables storage, each service just has a unique endpoint. These scenarios are explored using Python examples that illustrate how to:
26+
This sample shows you how to use the [Azure Data Tables SDK for Python](https://pypi.org/project/azure-data-tables/) in common Azure Table storage scenarios. The name of the SDK indicates it is for use with Azure Tables storage, but it works with both Azure Cosmos DB and Azure Tables storage, each service just has a unique endpoint. These scenarios are explored using Python examples that illustrate how to:
2727

2828
* Create and delete tables
2929
* Insert and query entities
3030
* Modify entities
3131

32-
While working through the scenarios in this sample, you may want to refer to the [Azure Cosmos DB SDK for Python API reference](/python/api/overview/azure/cosmosdb).
32+
While working through the scenarios in this sample, you may want to refer to the [Azure Data Tables SDK for Python API reference](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/tables/azure-data-tables).
3333

3434
## Prerequisites
3535

3636
You need the following to complete this sample successfully:
3737

3838
* [Python](https://www.python.org/downloads/) 2.7 or 3.6+.
39-
* [Azure Cosmos DB Table SDK for Python](https://pypi.python.org/pypi/azure-cosmosdb-table/). This SDK connects with both Azure Table storage and the Azure Cosmos DB Table API.
39+
* [Azure Data Tables SDK for Python](https://pypi.python.org/pypi/azure-data-tables/). This SDK connects with both Azure Table storage and the Azure Cosmos DB Table API.
4040
* [Azure Storage account](../../storage/common/storage-account-create.md) or [Azure Cosmos DB account](https://azure.microsoft.com/try/cosmosdb/).
4141

4242
## Create an Azure service account
@@ -51,64 +51,71 @@ You need the following to complete this sample successfully:
5151

5252
[!INCLUDE [cosmos-db-create-tableapi-account](../includes/cosmos-db-create-tableapi-account.md)]
5353

54-
## Install the Azure Cosmos DB Table SDK for Python
54+
## Install the Azure Data Tables SDK for Python
5555

56-
After you've created a Storage account, your next step is to install the [Microsoft Azure Cosmos DB Table SDK for Python](https://pypi.python.org/pypi/azure-cosmosdb-table/). For details on installing the SDK, refer to the [README.rst](https://github.com/Azure/azure-cosmosdb-python/blob/master/azure-cosmosdb-table/README.rst) file in the Cosmos DB Table SDK for Python repository on GitHub.
56+
After you've created a Storage account, your next step is to install the [Microsoft Azure Data Tables SDK for Python](https://pypi.python.org/pypi/azure-data-tables/). For details on installing the SDK, refer to the [README.md](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/tables/azure-data-tables/README.md) file in the Data Tables SDK for Python repository on GitHub.
5757

58-
## Import the TableService and Entity classes
58+
## Import the TableServiceClient and TableEntity classes
5959

60-
To work with entities in the Azure Table service in Python, you use the [TableService][py_TableService] and [Entity][py_Entity] classes. Add this code near the top your Python file to import both:
60+
To work with entities in the Azure Data Tables service in Python, you use the `TableServiceClient` and `TableEntity` classes. Add this code near the top your Python file to import both:
6161

6262
```python
63-
from azure.cosmosdb.table.tableservice import TableService
64-
from azure.cosmosdb.table.models import Entity
63+
from azure.data.tables import TableServiceClient
64+
from azure.data.tables import TableEntity
6565
```
6666

6767
## Connect to Azure Table service
68+
You can either connect to the Azure Storage account or the Azure Cosmos DB Table API account. Get the shared key or connection string based on the type of account you are using.
6869

69-
To connect to Azure Storage Table service, create a [TableService][py_TableService] object, and pass in your Storage account name and account key. Replace `myaccount` and `mykey` with your account name and key.
70+
### Creating the Table service client from a shared key
71+
72+
Create a `TableServiceClient` object, and pass in your Cosmos DB or Storage account name, account key and table endpoint. Replace `myaccount`, `mykey` and `mytableendpoint` with your Cosmos DB or Storage account name, key and table endpoint.
7073

7174
```python
72-
table_service = TableService(account_name='myaccount', account_key='mykey')
75+
from azure.core.credentials import AzureNamedKeyCredential
76+
77+
credential = AzureNamedKeyCredential("myaccount", "mykey")
78+
table_service = TableServiceClient(endpoint="mytableendpoint", credential=credential)
7379
```
7480

75-
## Connect to Azure Cosmos DB
81+
### Creating the Table service client from a connection string
7682

77-
To connect to Azure Cosmos DB, copy your primary connection string from the Azure portal, and create a [TableService][py_TableService] object using your copied connection string:
83+
Copy your Cosmos DB or Storage account connection string from the Azure portal, and create a `TableServiceClient` object using your copied connection string:
7884

7985
```python
80-
table_service = TableService(connection_string='DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey;TableEndpoint=myendpoint;')
86+
table_service = TableServiceClient.from_connection_string(conn_str='DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey;TableEndpoint=mytableendpoint;')
8187
```
8288

8389
## Create a table
8490

85-
Call [create_table][py_create_table] to create the table.
91+
Call `create_table` to create the table.
8692

8793
```python
8894
table_service.create_table('tasktable')
8995
```
9096

9197
## Add an entity to a table
9298

93-
To add an entity, you first create an object that represents your entity, then pass the object to the [TableService.insert_entity method][py_TableService]. The entity object can be a dictionary or an object of type [Entity][py_Entity], and defines your entity's property names and values. Every entity must include the required [PartitionKey and RowKey](#partitionkey-and-rowkey) properties, in addition to any other properties you define for the entity.
99+
Create a table in your account and get a `TableClient` to perform operations on the newly created table. To add an entity, you first create an object that represents your entity, then pass the object to the `TableClient.create_entity` method. The entity object can be a dictionary or an object of type `TableEntity`, and defines your entity's property names and values. Every entity must include the required [PartitionKey and RowKey](#partitionkey-and-rowkey) properties, in addition to any other properties you define for the entity.
94100

95-
This example creates a dictionary object representing an entity, then passes it to the [insert_entity][py_insert_entity] method to add it to the table:
101+
This example creates a dictionary object representing an entity, then passes it to the `create_entity` method to add it to the table:
96102

97103
```python
98-
task = {'PartitionKey': 'tasksSeattle', 'RowKey': '001',
99-
'description': 'Take out the trash', 'priority': 200}
100-
table_service.insert_entity('tasktable', task)
104+
table_client = table_service.get_table_client(table_name="tasktable")
105+
task = {u'PartitionKey': u'tasksSeattle', u'RowKey': u'001',
106+
u'description': u'Take out the trash', u'priority': 200}
107+
table_client.create_entity(entity=task)
101108
```
102109

103-
This example creates an [Entity][py_Entity] object, then passes it to the [insert_entity][py_insert_entity] method to add it to the table:
110+
This example creates an `TableEntity` object, then passes it to the `create_entity` method to add it to the table:
104111

105112
```python
106-
task = Entity()
107-
task.PartitionKey = 'tasksSeattle'
108-
task.RowKey = '002'
109-
task.description = 'Wash the car'
110-
task.priority = 100
111-
table_service.insert_entity('tasktable', task)
113+
task = TableEntity()
114+
task[u'PartitionKey'] = u'tasksSeattle'
115+
task[u'RowKey'] = u'002'
116+
task[u'description'] = u'Wash the car'
117+
task[u'priority'] = 100
118+
table_client.create_entity(task)
112119
```
113120

114121
### PartitionKey and RowKey
@@ -119,82 +126,65 @@ The Table service uses **PartitionKey** to intelligently distribute table entiti
119126

120127
## Update an entity
121128

122-
To update all of an entity's property values, call the [update_entity][py_update_entity] method. This example shows how to replace an existing entity with an updated version:
129+
To update all of an entity's property values, call the `update_entity` method. This example shows how to replace an existing entity with an updated version:
123130

124131
```python
125-
task = {'PartitionKey': 'tasksSeattle', 'RowKey': '001',
126-
'description': 'Take out the garbage', 'priority': 250}
127-
table_service.update_entity('tasktable', task)
132+
task = {u'PartitionKey': u'tasksSeattle', u'RowKey': u'001',
133+
u'description': u'Take out the garbage', u'priority': 250}
134+
table_client.update_entity(task)
128135
```
129136

130-
If the entity that is being updated doesn't already exist, then the update operation will fail. If you want to store an entity whether it exists or not, use [insert_or_replace_entity][py_insert_or_replace_entity]. In the following example, the first call will replace the existing entity. The second call will insert a new entity, since no entity with the specified PartitionKey and RowKey exists in the table.
137+
If the entity that is being updated doesn't already exist, then the update operation will fail. If you want to store an entity whether it exists or not, use `upsert_entity`. In the following example, the first call will replace the existing entity. The second call will insert a new entity, since no entity with the specified PartitionKey and RowKey exists in the table.
131138

132139
```python
133140
# Replace the entity created earlier
134-
task = {'PartitionKey': 'tasksSeattle', 'RowKey': '001',
135-
'description': 'Take out the garbage again', 'priority': 250}
136-
table_service.insert_or_replace_entity('tasktable', task)
141+
task = {u'PartitionKey': u'tasksSeattle', u'RowKey': u'001',
142+
u'description': u'Take out the garbage again', u'priority': 250}
143+
table_client.upsert_entity(task)
137144

138145
# Insert a new entity
139-
task = {'PartitionKey': 'tasksSeattle', 'RowKey': '003',
140-
'description': 'Buy detergent', 'priority': 300}
141-
table_service.insert_or_replace_entity('tasktable', task)
146+
task = {u'PartitionKey': u'tasksSeattle', u'RowKey': u'003',
147+
u'description': u'Buy detergent', u'priority': 300}
148+
table_client.upsert_entity(task)
142149
```
143150

144151
> [!TIP]
145-
> The [update_entity][py_update_entity] method replaces all properties and values of an existing entity, which you can also use to remove properties from an existing entity. You can use the [merge_entity][py_merge_entity] method to update an existing entity with new or modified property values without completely replacing the entity.
152+
> The **mode=UpdateMode.REPLACE** parameter in `update_entity` method replaces all properties and values of an existing entity, which you can also use to remove properties from an existing entity. The **mode=UpdateMode.MERGE** parameter is used by default to update an existing entity with new or modified property values without completely replacing the entity.
146153
147154
## Modify multiple entities
148155

149-
To ensure the atomic processing of a request by the Table service, you can submit multiple operations together in a batch. First, use the [TableBatch][py_TableBatch] class to add multiple operations to a single batch. Next, call [TableService][py_TableService].[commit_batch][py_commit_batch] to submit the operations in an atomic operation. All entities to be modified in batch must be in the same partition.
156+
To ensure the atomic processing of a request by the Table service, you can submit multiple operations together in a batch. First, add multiple operations to a list. Next, call `Table_client.submit_transaction` to submit the operations in an atomic operation. All entities to be modified in batch must be in the same partition.
150157

151158
This example adds two entities together in a batch:
152159

153160
```python
154-
from azure.cosmosdb.table.tablebatch import TableBatch
155-
batch = TableBatch()
156-
task004 = {'PartitionKey': 'tasksSeattle', 'RowKey': '004',
157-
'description': 'Go grocery shopping', 'priority': 400}
158-
task005 = {'PartitionKey': 'tasksSeattle', 'RowKey': '005',
159-
'description': 'Clean the bathroom', 'priority': 100}
160-
batch.insert_entity(task004)
161-
batch.insert_entity(task005)
162-
table_service.commit_batch('tasktable', batch)
163-
```
164-
165-
Batches can also be used with the context manager syntax:
166-
167-
```python
168-
task006 = {'PartitionKey': 'tasksSeattle', 'RowKey': '006',
169-
'description': 'Go grocery shopping', 'priority': 400}
170-
task007 = {'PartitionKey': 'tasksSeattle', 'RowKey': '007',
171-
'description': 'Clean the bathroom', 'priority': 100}
172-
173-
with table_service.batch('tasktable') as batch:
174-
batch.insert_entity(task006)
175-
batch.insert_entity(task007)
161+
task004 = {u'PartitionKey': u'tasksSeattle', u'RowKey': '004',
162+
'description': u'Go grocery shopping', u'priority': 400}
163+
task005 = {u'PartitionKey': u'tasksSeattle', u'RowKey': '005',
164+
u'description': u'Clean the bathroom', u'priority': 100}
165+
operations = [("create", task004), ("create", task005)]
166+
table_client.submit_transaction(operations)
176167
```
177168

178169
## Query for an entity
179170

180-
To query for an entity in a table, pass its PartitionKey and RowKey to the [TableService][py_TableService].[get_entity][py_get_entity] method.
171+
To query for an entity in a table, pass its PartitionKey and RowKey to the `Table_client.get_entity` method.
181172

182173
```python
183-
task = table_service.get_entity('tasktable', 'tasksSeattle', '001')
184-
print(task.description)
185-
print(task.priority)
174+
task = table_client.get_entity('tasksSeattle', '001')
175+
print(task['description'])
176+
print(task['priority'])
186177
```
187178

188179
## Query a set of entities
189180

190-
You can query for a set of entities by supplying a filter string with the **filter** parameter. This example finds all tasks in Seattle by applying a filter on PartitionKey:
181+
You can query for a set of entities by supplying a filter string with the **query_filter** parameter. This example finds all tasks in Seattle by applying a filter on PartitionKey:
191182

192183
```python
193-
tasks = table_service.query_entities(
194-
'tasktable', filter="PartitionKey eq 'tasksSeattle'")
184+
tasks = table_client.query_entities(query_filter="PartitionKey eq 'tasksSeattle'")
195185
for task in tasks:
196-
print(task.description)
197-
print(task.priority)
186+
print(task['description'])
187+
print(task['priority'])
198188
```
199189

200190
## Query a subset of entity properties
@@ -207,35 +197,34 @@ The query in the following code returns only the descriptions of entities in the
207197
> The following snippet works only against the Azure Storage. It is not supported by the Storage Emulator.
208198
209199
```python
210-
tasks = table_service.query_entities(
211-
'tasktable', filter="PartitionKey eq 'tasksSeattle'", select='description')
200+
tasks = table_client.query_entities(
201+
query_filter="PartitionKey eq 'tasksSeattle'", select='description')
212202
for task in tasks:
213-
print(task.description)
203+
print(task['description'])
214204
```
215205

216206
## Query for an entity without partition and row keys
217207

218-
You can also query for entities within a table without using the partition and row keys. Use the `table_service.query_entities` method without the "filter" and "select" parameters as show in the following example:
208+
You can also list entities within a table without using the partition and row keys. Use the `table_client.list_entities` method as show in the following example:
219209

220210
```python
221211
print("Get the first item from the table")
222-
tasks = table_service.query_entities(
223-
'tasktable')
212+
tasks = table_client.list_entities()
224213
lst = list(tasks)
225214
print(lst[0])
226215
```
227216

228217
## Delete an entity
229218

230-
Delete an entity by passing its **PartitionKey** and **RowKey** to the [delete_entity][py_delete_entity] method.
219+
Delete an entity by passing its **PartitionKey** and **RowKey** to the `delete_entity` method.
231220

232221
```python
233-
table_service.delete_entity('tasktable', 'tasksSeattle', '001')
222+
table_client.delete_entity('tasksSeattle', '001')
234223
```
235224

236225
## Delete a table
237226

238-
If you no longer need a table or any of the entities within it, call the [delete_table][py_delete_table] method to permanently delete the table from Azure Storage.
227+
If you no longer need a table or any of the entities within it, call the `delete_table` method to permanently delete the table from Azure Storage.
239228

240229
```python
241230
table_service.delete_table('tasktable')
@@ -244,22 +233,8 @@ table_service.delete_table('tasktable')
244233
## Next steps
245234

246235
* [FAQ - Develop with the Table API](table-api-faq.yml)
247-
* [Azure Cosmos DB SDK for Python API reference](/python/api/overview/azure/cosmosdb)
236+
* [Azure Data Tables SDK for Python API reference](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/tables/azure-data-tables)
248237
* [Python Developer Center](https://azure.microsoft.com/develop/python/)
249238
* [Microsoft Azure Storage Explorer](../../vs-azure-tools-storage-manage-with-storage-explorer.md): A free, cross-platform application for working visually with Azure Storage data on Windows, macOS, and Linux.
250239
* [Working with Python in Visual Studio (Windows)](/visualstudio/python/overview-of-python-tools-for-visual-studio)
251240

252-
253-
254-
[py_commit_batch]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice
255-
[py_create_table]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice
256-
[py_delete_entity]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice
257-
[py_get_entity]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice
258-
[py_insert_entity]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice
259-
[py_insert_or_replace_entity]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice
260-
[py_Entity]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.models.entity
261-
[py_merge_entity]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice
262-
[py_update_entity]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice
263-
[py_delete_table]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice
264-
[py_TableService]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice
265-
[py_TableBatch]: /python/api/azure-cosmosdb-table/azure.cosmosdb.table.tableservice.tableservice

0 commit comments

Comments
 (0)