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

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=Trueenable_cross_partition_query=Trueset the enable_scan_in_query=True and enable_cross_partition_query=True when querying the items