Редактиране

Споделяне чрез


Quickstart: Use Azure Cosmos DB for Table with Azure SDK for Python

In this quickstart, you deploy a basic Azure Cosmos DB for Table application using the Azure SDK for Python. Azure Cosmos DB for Table is a schemaless data store allowing applications to store structured table data in the cloud. You learn how to create tables, rows, and perform basic tasks within your Azure Cosmos DB resource using the Azure SDK for Python.

API reference documentation | Library source code | Package (PyPI) | Azure Developer CLI

Prerequisites

  • Azure Developer CLI
  • Docker Desktop
  • Python 3.12

If you don't have an Azure account, create a free account before you begin.

Initialize the project

Use the Azure Developer CLI (azd) to create an Azure Cosmos DB for Table account and deploy a containerized sample application. The sample application uses the client library to manage, create, read, and query sample data.

  1. Open a terminal in an empty directory.

  2. If you're not already authenticated, authenticate to the Azure Developer CLI using azd auth login. Follow the steps specified by the tool to authenticate to the CLI using your preferred Azure credentials.

    azd auth login
    
  3. Use azd init to initialize the project.

    azd init --template cosmos-db-table-python-quickstart
    
  4. During initialization, configure a unique environment name.

  5. Deploy the Azure Cosmos DB account using azd up. The Bicep templates also deploy a sample web application.

    azd up
    
  6. During the provisioning process, select your subscription, desired location, and target resource group. Wait for the provisioning process to complete. The process can take approximately five minutes.

  7. Once the provisioning of your Azure resources is done, a URL to the running web application is included in the output.

    Deploying services (azd deploy)
    
      (✓) Done: Deploying service web
    - Endpoint: <https://[container-app-sub-domain].azurecontainerapps.io>
    
    SUCCESS: Your application was provisioned and deployed to Azure in 5 minutes 0 seconds.
    
  8. Use the URL in the console to navigate to your web application in the browser. Observe the output of the running app.

Screenshot of the running web application.

Install the client library

The client library is available through PyPi, as the azure-data-tables package.

  1. Open a terminal and navigate to the /src folder.

    cd ./src
    
  2. If not already installed, install the azure-data-tables package using pip install.

    pip install azure-data-tables
    
  3. Open and review the src/requirements.txt file to validate that the azure-data-tables entry exists.

Object model

Name Description
TableServiceClient This type is the primary client type and is used to manage account-wide metadata or databases.
TableClient This type represents the client for a table within the account.

Code examples

The sample code in the template uses a table named cosmicworks-products. The cosmicworks-products table contains details such as name, category, quantity, price, a unique identifier, and a sale flag for each product. The container uses a unique identifier as the row key and category as a partition key.

Authenticate the client

This sample creates a new instance of the TableServiceClient type.

credential = DefaultAzureCredential()

client = TableServiceClient(endpoint="<azure-cosmos-db-table-account-endpoint>", credential=credential)

Get a table

This sample creates an instance of the TableClient type using the GetTableClient function of the TableServiceClient type.

table = client.get_table_client("<azure-cosmos-db-table-name>")

Create an entity

The easiest way to create a new entity in a table is to create a new object ensuring that you specify the mandatory RowKey and PartitionKey properties.

new_entity = {
    "RowKey": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
    "PartitionKey": "gear-surf-surfboards",
    "Name": "Yamba Surfboard",
    "Quantity": 12,
    "Sale": False,
}

Create an entity in the table using upsert_entity.

created_entity = table.upsert_entity(new_entity)

Get an entity

You can retrieve a specific entity from a table using get_entity.

existing_entity = table.get_entity(
    row_key="aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
    partition_key="gear-surf-surfboards",
)

Query entities

After you insert an entity, you can also run a query to get all entities that match a specific filter by using query_entities with a string OData filter.

category = "gear-surf-surfboards"
filter = f"PartitionKey eq '{category}'"
entities = table.query_entities(query_filter=filter)

Parse the paginated results of the query by using a for loop.

for entity in entities:
    # Do something

Clean up resources

When you no longer need the sample application or resources, remove the corresponding deployment and all resources.

azd down