You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -23,20 +23,20 @@ You can use the Table storage or the Azure Cosmos DB to store flexible datasets
23
23
24
24
### About this sample
25
25
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:
27
27
28
28
* Create and delete tables
29
29
* Insert and query entities
30
30
* Modify entities
31
31
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).
33
33
34
34
## Prerequisites
35
35
36
36
You need the following to complete this sample successfully:
37
37
38
38
*[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.
40
40
*[Azure Storage account](../../storage/common/storage-account-create.md) or [Azure Cosmos DB account](https://azure.microsoft.com/try/cosmosdb/).
41
41
42
42
## Create an Azure service account
@@ -51,64 +51,71 @@ You need the following to complete this sample successfully:
## Install the Azure Cosmos DB Table SDK for Python
54
+
## Install the Azure Data Tables SDK for Python
55
55
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.
57
57
58
-
## Import the TableService and Entity classes
58
+
## Import the TableServiceClient and TableEntity classes
59
59
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:
61
61
62
62
```python
63
-
from azure.cosmosdb.table.tableserviceimportTableService
64
-
from azure.cosmosdb.table.modelsimportEntity
63
+
from azure.data.tablesimportTableServiceClient
64
+
from azure.data.tablesimportTableEntity
65
65
```
66
66
67
67
## 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.
68
69
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.
### Creating the Table service client from a connection string
76
82
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:
Call [create_table][py_create_table] to create the table.
91
+
Call `create_table` to create the table.
86
92
87
93
```python
88
94
table_service.create_table('tasktable')
89
95
```
90
96
91
97
## Add an entity to a table
92
98
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.
94
100
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:
u'description': u'Take out the trash', u'priority': 200}
107
+
table_client.create_entity(entity=task)
101
108
```
102
109
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:
104
111
105
112
```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)
112
119
```
113
120
114
121
### PartitionKey and RowKey
@@ -119,82 +126,65 @@ The Table service uses **PartitionKey** to intelligently distribute table entiti
119
126
120
127
## Update an entity
121
128
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:
u'description': u'Take out the garbage', u'priority': 250}
134
+
table_client.update_entity(task)
128
135
```
129
136
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.
> 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.
146
153
147
154
## Modify multiple entities
148
155
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.
150
157
151
158
This example adds two entities together in a batch:
152
159
153
160
```python
154
-
from azure.cosmosdb.table.tablebatch import TableBatch
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:
## Query for an entity without partition and row keys
217
207
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:
219
209
220
210
```python
221
211
print("Get the first item from the table")
222
-
tasks = table_service.query_entities(
223
-
'tasktable')
212
+
tasks = table_client.list_entities()
224
213
lst =list(tasks)
225
214
print(lst[0])
226
215
```
227
216
228
217
## Delete an entity
229
218
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.
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.
*[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.
250
239
*[Working with Python in Visual Studio (Windows)](/visualstudio/python/overview-of-python-tools-for-visual-studio)
0 commit comments