Azure Cosmos DB Python 示例Azure Cosmos DB Python examples

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

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

可以使用 URI 为 https://localhost:8081Azure Cosmos DB 模拟器You can use the Azure Cosmos DB Emulator with a URI of https://localhost:8081. 有关在模拟器中使用的密钥,请参阅对请求进行身份验证For the key to use with the emulator, see Authenticating requests.

还需要 Python SDKYou also need the Python SDK.

备注

每个示例都是独立的,自行对自身进行设置并在完成后自行进行清理。Each sample is self-contained; it sets itself up and cleans up after itself. 这些示例对 CosmosClient.CreateContainer 发出多个调用。The samples issue multiple calls to CosmosClient.CreateContainer. 每次调用完成后,你的订阅将按使用一小时计费。Each time this is done, your subscription is billed for one hour of usage. 有关 Azure Cosmos DB 计费的详细信息,请参阅 Azure Cosmos DB 定价For more information about Azure Cosmos DB billing, see Azure Cosmos DB Pricing.

数据库示例Database examples

DatabaseManagement 项目的 Program.py 文件演示了如何执行以下任务。The Program.py file of the DatabaseManagement project 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.CreateDatabaseCosmosClient.CreateDatabase
按 ID 读取数据库Read a database by ID CosmosClient.ReadDatabaseCosmosClient.ReadDatabase
列出帐户的数据库List databases for an account CosmosClient.ReadDatabasesCosmosClient.ReadDatabases
删除数据库Delete a database CosmosClient.DeleteDatabaseCosmosClient.DeleteDatabase

集合示例Collection examples

CollectionManagement 项目的 Program.py 文件演示了如何执行以下任务。The Program.py file of the CollectionManagement project 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
创建集合Create a collection CosmosClient.CreateContainerCosmosClient.CreateContainer
读取数据库中所有集合的列表Read a list of all collections in a database CosmosClient.ReadContainersCosmosClient.ReadContainers
按 ID 获取集合Get a collection by ID CosmosClient.ReadContainerCosmosClient.ReadContainer
更改集合的吞吐量Change the throughput of a collection CosmosClient.ReplaceOfferCosmosClient.ReplaceOffer
删除集合Delete a collection CosmosClient.DeleteContainerCosmosClient.DeleteContainer

文档示例Document examples

DocumentManagement 项目的 Program.py 文件演示了如何执行以下任务。The Program.py file of the DocumentManagement project 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 a document CosmosClient.CreateItemCosmosClient.CreateItem
创建文档的集合Create a collection of documents CosmosClient.CreateItemCosmosClient.CreateItem
按 ID 读取文档Read a document by ID CosmosClient.ReadItemCosmosClient.ReadItem
Read all the documents in a collectionRead all the documents in a collection CosmosClient.ReadItemsCosmosClient.ReadItems
使用条件 ETag 检查替换文档Replace document with conditional ETag check CosmosClient.ReplaceItemCosmosClient.ReplaceItem

索引示例Indexing examples

IndexManagement 项目的 Program.py 文件演示了如何执行以下任务。The Program.py file of the IndexManagement project 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
使用手动(而非自动)索引Use manual (instead of automatic) indexing 自动索引编制策略Automatic indexing policy
从索引中排除指定的文档路径Exclude specified document paths from the index 基于排除的路径的索引编制策略Indexing policy with excluded paths
从索引中排除文档Exclude a document from the index IndexingDirective.ExcludeIndexingDirective.Exclude
设置索引模式Set indexing mode IndexingModeIndexingMode
对字符串使用范围索引Use range indexes on strings 基于包括的路径的索引编制策略Indexing policy with included paths
执行索引转换Perform an index transformation CosmosClient.ReplaceContainerCosmosClient.ReplaceContainer

查询示例Query examples

示例项目还演示了如何执行以下查询任务。The sample projects also show how to do the following query tasks. 若要在运行以下示例之前了解 Azure Cosmos DB 中的 SQL 查询引用,请参阅概念文章:SQL 查询示例To learn about the SQL query reference in Azure Cosmos DB before running the following samples, see SQL query examples conceptual article. 若要在运行以下示例之前了解 Azure Cosmos DB 中的 SQL 查询引用,请参阅概念文章:SQL 查询示例To learn about the SQL query reference in Azure Cosmos DB before running the following samples, see SQL query examples conceptual article.

任务Task API 参考API reference
查询数据库帐户Query an account for a database CosmosClient.QueryDatabasesCosmosClient.QueryDatabases
查询文档Query for documents CosmosClient.QueryItemsCosmosClient.QueryItems
对哈希索引路径强制执行范围扫描操作Force a range scan operation on a hash indexed path HttpHeaders.EnableScanInQueryHttpHeaders.EnableScanInQuery