title | description | services | documentationcenter | author | manager | editor | ms.assetid | ms.service | ms.custom | ms.workload | ms.tgt_pltfrm | ms.devlang | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Azure Cosmos DB: Build an app with Python and the DocumentDB API | Microsoft Docs |
Presents a Python code sample you can use to connect to and query the Azure Cosmos DB DocumentDB API |
cosmos-db |
mimig1 |
jhubbard |
51c11be2-af6d-425f-a86a-39cbfe61da29 |
cosmos-db |
quick start connect, mvc, devcenter |
na |
python |
quickstart |
10/16/2017 |
mimig |
Azure Cosmos DB is Microsoft’s globally distributed multi-model database service. You can quickly create and query document, key/value, and graph databases, all of which benefit from the global distribution and horizontal scale capabilities at the core of Azure Cosmos DB.
This quick start demonstrates how to create an Azure Cosmos DB account, document database, and collection using the Azure portal. You then build and run a console app built on the DocumentDB Python API.
- Before you can run this sample, you must have the following prerequisites:
- If you don’t already have Visual Studio 2017 installed, you can download and use the free Visual Studio 2017 Community Edition. Make sure that you enable Azure development during the Visual Studio setup.
- Python Tools for Visual Studio from GitHub. This tutorial uses Python Tools for VS 2015.
- Python 2.7 from python.org
[!INCLUDE quickstarts-free-trial-note] [!INCLUDE cosmos-db-emulator-docdb-api]
[!INCLUDE cosmos-db-create-dbaccount]
[!INCLUDE cosmos-db-create-collection]
Now let's clone a DocumentDB API app from github, set the connection string, and run it. You see how easy it is to work with data programmatically.
-
Open a git terminal window, such as git bash, and
cd
to a working directory. -
Run the following command to clone the sample repository.
git clone https://github.com/Azure-Samples/azure-cosmos-db-documentdb-python-getting-started.git
Let's make a quick review of what's happening in the app. Open the DocumentDBGetStarted.py file and you'll find that these lines of code create the Azure Cosmos DB resources.
-
The DocumentClient is initialized.
# Initialize the Python DocumentDB client client = document_client.DocumentClient(config['ENDPOINT'], {'masterKey': config['MASTERKEY']})
-
A new database is created.
# Create a database db = client.CreateDatabase({ 'id': config['DOCUMENTDB_DATABASE'] })
-
A new collection is created.
# Create collection options options = { 'offerEnableRUPerMinuteThroughput': True, 'offerVersion': "V2", 'offerThroughput': 400 } # Create a collection collection = client.CreateCollection(db['_self'], { 'id': config['DOCUMENTDB_COLLECTION'] }, options)
-
Some documents are created.
# Create some documents document1 = client.CreateDocument(collection['_self'], { 'id': 'server1', 'Web Site': 0, 'Cloud Service': 0, 'Virtual Machine': 0, 'name': 'some' })
-
A query is performed using SQL
# Query them in SQL query = { 'query': 'SELECT * FROM server s' } options = {} options['enableCrossPartitionQuery'] = True options['maxItemCount'] = 2 result_iterable = client.QueryDocuments(collection['_self'], query, options) results = list(result_iterable); print(results)
Now go back to the Azure portal to get your connection string information and copy it into the app.
-
In the Azure portal, in your Azure Cosmos DB account, in the left navigation click Keys, and then click Read-write Keys. You'll use the copy buttons on the right side of the screen to copy the URI and Primary Key into the
DocumentDBGetStarted.py
file in the next step. -
In Open the
DocumentDBGetStarted.py
file. -
Copy your URI value from the portal (using the copy button) and make it the value of the endpoint key in
DocumentDBGetStarted.py
.'ENDPOINT': 'https://FILLME.documents.azure.com',
-
Then copy your PRIMARY KEY value from the portal and make it the value of the
config.MASTERKEY
inDocumentDBGetStarted.py
. You've now updated your app with all the info it needs to communicate with Azure Cosmos DB.'MASTERKEY': 'FILLME',
-
In Visual Studio, right-click on the project in Solution Explorer, select the current Python environment, then right click.
-
Select Install Python Package, then type in pydocumentdb
-
Run F5 to run the application. Your app displays in your browser.
You can now go back to Data Explorer and see query, modify, and work with this new data.
[!INCLUDE cosmosdb-tutorial-review-slas]
If you're not going to continue to use this app, delete all resources created by this quickstart in the Azure portal with the following steps:
- From the left-hand menu in the Azure portal, click Resource groups and then click the name of the resource you created.
- On your resource group page, click Delete, type the name of the resource to delete in the text box, and then click Delete.
In this quickstart, you've learned how to create an Azure Cosmos DB account, create a collection using the Data Explorer, and run an app. You can now import additional data to your Cosmos DB account.
[!div class="nextstepaction"] Import data into Azure Cosmos DB for the DocumentDB API