.. role:: raw-html-m2r(raw) :format: html Azure Cosmos DB SQL API client library for Python ================================================= Azure Cosmos DB is a globally distributed, multi-model database service that supports document, key-value, wide-column, and graph databases. Use the Azure Cosmos DB SQL API SDK for Python to manage databases and the JSON documents they contain in this NoSQL database service. * Create Cosmos DB **databases** and modify their settings * Create and modify **containers** to store collections of JSON documents * Create, read, update, and delete the **items** (JSON documents) in your containers * Query the documents in your database using **SQL-like syntax** `SDK source code `_ | `Package (PyPI) `_ | `API reference documentation `_ | `Product documentation `_ | `Samples `_ Getting started --------------- Prerequisites ^^^^^^^^^^^^^ * Azure subscription - `Create a free account `_ * Azure `Cosmos DB account `_ - SQL API * `Python 2.7 or 3.5.3+ `_ If you need a Cosmos DB SQL API account, you can create one with this `Azure CLI `_ command: .. code-block:: Bash az cosmosdb create --resource-group --name Install the package ^^^^^^^^^^^^^^^^^^^ .. code-block:: bash pip install azure-cosmos Configure a virtual environment (optional) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Although not required, you can keep your your base system and Azure SDK environments isolated from one another if you use a virtual environment. Execute the following commands to configure and then enter a virtual environment with `venv `_\ : .. code-block:: Bash python3 -m venv azure-cosmosdb-sdk-environment source azure-cosmosdb-sdk-environment/bin/activate Authenticate the client ^^^^^^^^^^^^^^^^^^^^^^^ Interaction with Cosmos DB starts with an instance of the `CosmosClient `_ class. You need an **account**\ , its **URI**\ , and one of its **account keys** to instantiate the client object. Use the Azure CLI snippet below to populate two environment variables with the database account URI and its primary master key (you can also find these values in the Azure portal). The snippet is formatted for the Bash shell. .. code-block:: Bash RES_GROUP= ACCT_NAME= export ACCOUNT_URI=$(az cosmosdb show --resource-group $RES_GROUP --name $ACCT_NAME --query documentEndpoint --output tsv) export ACCOUNT_KEY=$(az cosmosdb list-keys --resource-group $RES_GROUP --name $ACCT_NAME --query primaryMasterKey --output tsv) Create the client ^^^^^^^^^^^^^^^^^ Once you've populated the ``ACCOUNT_URI`` and ``ACCOUNT_KEY`` environment variables, you can create the `CosmosClient `_. .. code-block:: Python from azure.cosmos import CosmosClient, PartitionKey, exceptions import os url = os.environ['ACCOUNT_URI'] key = os.environ['ACCOUNT_KEY'] client = CosmosClient(url, credential=key) Key concepts ------------ Once you've initialized a `CosmosClient `_\ , you can interact with the primary resource types in Cosmos DB: * `Database `_\ : A Cosmos DB account can contain multiple databases. When you create a database, you specify the API you'd like to use when interacting with its documents: SQL, MongoDB, Gremlin, Cassandra, or Azure Table. Use the `DatabaseProxy `_ object to manage its containers. * `Container `_\ : A container is a collection of JSON documents. You create (insert), read, update, and delete items in a container by using methods on the `ContainerProxy `_ object. * Item: An Item is the dictionary-like representation of a JSON document stored in a container. Each Item you add to a container must include an ``id`` key with a value that uniquely identifies the item within the container. For more information about these resources, see `Working with Azure Cosmos databases, containers and items `_. Examples -------- The following sections provide several code snippets covering some of the most common Cosmos DB tasks, including: * :raw-html-m2r:`Create a database` * :raw-html-m2r:`Create a container` * :raw-html-m2r:`Get an existing container` * :raw-html-m2r:`Insert data` * :raw-html-m2r:`Delete data` * :raw-html-m2r:`Query the database` * :raw-html-m2r:`Get database properties` * :raw-html-m2r:`Modify container properties` Create a database ^^^^^^^^^^^^^^^^^ After authenticating your `CosmosClient `_\ , you can work with any resource in the account. The code snippet below creates a SQL API database, which is the default when no API is specified when `create_database `_ is invoked. .. code-block:: Python database_name = 'testDatabase' try: database = client.create_database(database_name) except exceptions.CosmosResourceExistsError: database = client.get_database_client(database_name) Create a container ^^^^^^^^^^^^^^^^^^ This example creates a container with default settings. If a container with the same name already exists in the database (generating a ``409 Conflict`` error), the existing container is obtained instead. .. code-block:: Python container_name = 'products' try: container = database.create_container(id=container_name, partition_key=PartitionKey(path="/productName")) except exceptions.CosmosResourceExistsError: container = database.get_container_client(container_name) except exceptions.CosmosHttpResponseError: raise The preceding snippet also handles the `CosmosHttpResponseError `_ exception if the container creation failed. For more information on error handling and troubleshooting, see the :raw-html-m2r:`Troubleshooting` section. Get an existing container ^^^^^^^^^^^^^^^^^^^^^^^^^ Retrieve an existing container from the database: .. code-block:: Python database = client.get_database_client(database_name) container = database.get_container_client(container_name) Insert data ^^^^^^^^^^^ To insert items into a container, pass a dictionary containing your data to `ContainerProxy.upsert_item `_. Each item you add to a container must include an ``id`` key with a value that uniquely identifies the item within the container. This example inserts several items into the container, each with a unique ``id``\ : .. code-block:: Python database_client = client.get_database_client(database_name) container_client = database.get_container_client(container_name) for i in range(1, 10): container_client.upsert_item({ 'id': 'item{0}'.format(i), 'productName': 'Widget', 'productModel': 'Model {0}'.format(i) } ) Delete data ^^^^^^^^^^^ To delete items from a container, use `ContainerProxy.delete_item `_. The SQL API in Cosmos DB does not support the SQL ``DELETE`` statement. .. code-block:: Python for item in container.query_items( query='SELECT * FROM products p WHERE p.productModel = "DISCONTINUED"', enable_cross_partition_query=True): container.delete_item(item, partition_key='Pager') .. NOTE: If you are using partitioned collection, the value of the ``partitionKey`` in the example code above, should be set to the value of the partition key for this particular item, not the name of the partition key column in your collection. This holds true for both point reads and deletes. Query the database ^^^^^^^^^^^^^^^^^^ A Cosmos DB SQL API database supports querying the items in a container with `ContainerProxy.query_items `_ using SQL-like syntax. This example queries a container for items with a specific ``id``\ : .. code-block:: Python database = client.get_database_client(database_name) container = database.get_container_client(container_name) # Enumerate the returned items import json for item in container.query_items( query='SELECT * FROM mycontainer r WHERE r.id="item3"', enable_cross_partition_query=True): print(json.dumps(item, indent=True)) .. NOTE: Although you can specify any value for the container name in the ``FROM`` clause, we recommend you use the container name for consistency. Perform parameterized queries by passing a dictionary containing the parameters and their values to `ContainerProxy.query_items `_\ : .. code-block:: Python discontinued_items = container.query_items( query='SELECT * FROM products p WHERE p.productModel = @model', parameters=[ dict(name='@model', value='Model 7') ], enable_cross_partition_query=True ) for item in discontinued_items: print(json.dumps(item, indent=True)) For more information on querying Cosmos DB databases using the SQL API, see `Query Azure Cosmos DB data with SQL queries `_. Get database properties ^^^^^^^^^^^^^^^^^^^^^^^ Get and display the properties of a database: .. code-block:: Python database = client.get_database_client(database_name) properties = database.read() print(json.dumps(properties)) Modify container properties ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Certain properties of an existing container can be modified. This example sets the default time to live (TTL) for items in the container to 10 seconds: .. code-block:: Python database = client.get_database_client(database_name) container = database.get_container_client(container_name) database.replace_container( container, partition_key=PartitionKey(path="/productName"), default_ttl=10, ) # Display the new TTL setting for the container container_props = container.read() print(json.dumps(container_props['defaultTtl'])) For more information on TTL, see `Time to Live for Azure Cosmos DB data `_. Troubleshooting --------------- General ^^^^^^^ When you interact with Cosmos DB using the Python SDK, exceptions returned by the service correspond to the same HTTP status codes returned for REST API requests: `HTTP Status Codes for Azure Cosmos DB `_ For example, if you try to create a container using an ID (name) that's already in use in your Cosmos DB database, a ``409`` error is returned, indicating the conflict. In the following snippet, the error is handled gracefully by catching the exception and displaying additional information about the error. .. code-block:: Python try: database.create_container(id=container_name, partition_key=PartitionKey(path="/productName") except exceptions.CosmosResourceExistsError: print("""Error creating container HTTP status code 409: The ID (name) provided for the container is already in use. The container name must be unique within the database.""") Logging ^^^^^^^ This library uses the standard `logging `_ library for logging. Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level. Detailed DEBUG level logging, including request/response bodies and unredacted headers, can be enabled on a client with the ``logging_enable`` argument: .. code-block:: python import sys import logging from azure.cosmos import CosmosClient # Create a logger for the 'azure' SDK logger = logging.getLogger('azure') logger.setLevel(logging.DEBUG) # Configure a console output handler = logging.StreamHandler(stream=sys.stdout) logger.addHandler(handler) # This client will log detailed information about its HTTP sessions, at DEBUG level client = CosmosClient(url, credential=key, logging_enable=True) Similarly, ``logging_enable`` can enable detailed logging for a single operation, even when it isn't enabled for the client: .. code-block:: py database = client.create_database(database_name, logging_enable=True) Next steps ---------- For more extensive documentation on the Cosmos DB service, see the `Azure Cosmos DB documentation `_ on docs.microsoft.com. :raw-html-m2r:`` Contributing ============ This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com. When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA. This project has adopted the `Microsoft Open Source Code of Conduct `_. For more information see the `Code of Conduct FAQ `_ or contact `opencode@microsoft.com `_ with any additional questions or comments. Indices and tables ------------------ * :ref:`genindex` * :ref:`modindex` * :ref:`search` .. toctree:: :maxdepth: 5 :glob: :caption: Developer Documentation azure.cosmos.rst