Azure Cosmos DB Python 示例Azure Cosmos DB Python examples
适用于:
SQL API
azure-documentdb-python GitHub 存储库中包含对 Azure Cosmos DB 资源执行 CRUD 操作和其他常见操作的示例解决方案。Sample solutions that do CRUD operations and other common operations on Azure Cosmos DB resources are included in the azure-documentdb-python GitHub repository. 本文将提供:This article provides:
- 每个 Python 示例项目文件中的任务链接。Links to the tasks in each of the Python example project files.
- 指向相关的 API 参考内容的链接。Links to the related API reference content.
先决条件Prerequisites
- 一个 Cosmos DB 帐户。A Cosmos DB Account. 选项包括:You options are:
具有 Azure 有效订阅:Within an Azure active subscription:
- 创建 Azure 免费帐户或使用现有订阅Create an Azure free Account or use your existing subscription
- Visual Studio 每月额度Visual Studio Monthly Credits
- Azure Cosmos DB 免费层Azure Cosmos DB Free Tier
不具有 Azure 有效订阅:Without an Azure active subscription:
- Python 2.7 或 3.5.3+,以及
PATH
中的python
可执行文件。Python 2.7 or 3.5.3+, with thepython
executable in yourPATH
. - Visual Studio Code。Visual Studio Code.
- 适用于 Visual Studio Code 的 Python 扩展。The Python extension for Visual Studio Code.
- Git。Git.
- 用于 Python 的 Azure Cosmos DB SQL API SDKAzure Cosmos DB SQL API SDK for Python
数据库示例Database examples
database_management.py Python 示例演示如何执行以下任务。The database_management.py Python sample shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos 数据库,请参阅使用数据库、容器和项概念性文章。To learn about the Azure Cosmos databases before running the following samples, see Working with databases, containers, and items conceptual article.
任务Task | API 参考API reference |
---|---|
创建数据库Create a database | CosmosClient.create_databaseCosmosClient.create_database |
按 ID 读取数据库Read a database by ID | CosmosClient.get_database_clientCosmosClient.get_database_client |
查询数据库Query the databases | CosmosClient.query_databasesCosmosClient.query_databases |
列出帐户的数据库List databases for an account | CosmosClient.list_databasesCosmosClient.list_databases |
删除数据库Delete a database | CosmosClient.delete_databaseCosmosClient.delete_database |
容器示例Container examples
container_management.py Python 示例演示如何执行以下任务。The container_management.py Python sample shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos 集合,请参阅使用数据库、容器和项概念性文章。To learn about the Azure Cosmos collections before running the following samples, see Working with databases, containers, and items conceptual article.
任务Task | API 参考API reference |
---|---|
查询容器Query for a container | database.query_containersdatabase.query_containers |
创建容器Create a container | database.create_containerdatabase.create_container |
列出数据库中的所有容器List all the containers in a database | database.list_containersdatabase.list_containers |
按 ID 获取容器Get a container by its ID | database.get_container_clientdatabase.get_container_client |
管理容器的预配吞吐量Manage container's provisioned throughput | container.read_offer、container.replace_throughputcontainer.read_offer, container.replace_throughput |
删除容器Delete a container | database.delete_containerdatabase.delete_container |
项示例Item examples
item_management.py Python 示例演示如何执行以下任务。The item_management.py Python sample shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos 文档,请参阅使用数据库、容器和项概念性文章。To learn about the Azure Cosmos documents before running the following samples, see Working with databases, containers, and items conceptual article.
任务Task | API 参考API reference |
---|---|
在容器中创建项Create items in a container | container.create_itemcontainer.create_item |
按 ID 读取项Read an item by its ID | container.read_itemcontainer.read_item |
读取容器中的所有项Read all the items in a container | container.read_all_itemscontainer.read_all_items |
按 ID 查询项Query an item by its ID | container.query_itemscontainer.query_items |
替换项Replace an item | container.replace_itemscontainer.replace_items |
更新插入项Upsert an item | container.upsert_itemcontainer.upsert_item |
删除项Delete an item | container.delete_itemcontainer.delete_item |
获取容器中项的更改源Get the change feed of items in a container | container.query_items_change_feedcontainer.query_items_change_feed |
索引示例Indexing examples
index_management.py Python 示例演示如何执行以下任务。The index_management.py Python sample shows how to do the following tasks. 若要在运行以下示例之前了解 Azure Cosmos DB 中的索引,请参阅索引策略、索引类型和索引路径概念文章。To learn about indexing in Azure Cosmos DB before running the following samples, see indexing policies, indexing types, and indexing paths conceptual articles.
任务Task | API 参考API reference |
---|---|
将特定项排除在索引范围外Exclude a specific item from indexing | documents.IndexingDirective.Excludedocuments.IndexingDirective.Exclude |
对已编入索引的特定项使用手动索引Use manual indexing with specific items indexed | documents.IndexingDirective.Includedocuments.IndexingDirective.Include |
将路径排除在索引范围外Exclude paths from indexing | 定义要在 IndexingPolicy 属性中排除的路径Define paths to exclude in IndexingPolicy property |
对字符串使用范围索引Use range indexes on strings | 使用范围索引对字符串数据类型定义索引策略。Define indexing policy with range indexes on string data type. 'kind': documents.IndexKind.Range , 'dataType': documents.DataType.String 'kind': documents.IndexKind.Range , 'dataType': documents.DataType.String |
执行索引转换Perform an index transformation | database.replace_container(使用更新的索引策略)database.replace_container (use the updated indexing policy) |
当路径上仅存在哈希索引时使用扫描Use scans when only hash index exists on the path | 查询项时设置 enable_scan_in_query=True 和 enable_cross_partition_query=True set the enable_scan_in_query=True and enable_cross_partition_query=True when querying the items |