Azure Cosmos DB の Python サンプルAzure Cosmos DB Python examples
適用対象:
SQL API
Azure Cosmos DB のリソースで CRUD 操作などの一般的な操作を実行するサンプル ソリューションは、azure-documentdb-python GitHub リポジトリに含まれています。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 の Free レベルAzure Cosmos DB Free Tier
- Azure アクティブ サブスクリプションなしで:Without an Azure active subscription:
- 30 日間継続するテスト環境として、Azure Cosmos DB を無料で試す。Try Azure Cosmos DB for free, a tests environment that lasts for 30 days.
- Azure Cosmos DB EmulatorAzure Cosmos DB Emulator
- Azure アクティブ サブスクリプション内で:Within an Azure active subscription:
- Python 2.7 または 3.5.3 以降 (
python
実行可能ファイルがPATH
に設定されていること)。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.
- Azure Cosmos DB SQL API SDK for PythonAzure 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 |