Skip to content

Latest commit

 

History

History
164 lines (109 loc) · 6.33 KB

create-documentdb-python.md

File metadata and controls

164 lines (109 loc) · 6.33 KB
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: Build a DocumentDB API app with Python and the Azure portal

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.

Prerequisites

  • 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]

Create a database account

[!INCLUDE cosmos-db-create-dbaccount]

Add a collection

[!INCLUDE cosmos-db-create-collection]

Clone the sample application

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.

  1. Open a git terminal window, such as git bash, and cd to a working directory.

  2. Run the following command to clone the sample repository.

    git clone https://github.com/Azure-Samples/azure-cosmos-db-documentdb-python-getting-started.git

Review the code

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)

Update your connection string

Now go back to the Azure portal to get your connection string information and copy it into the app.

  1. 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.

    View and copy an access key in the Azure portal, Keys blade

  2. In Open the DocumentDBGetStarted.py file.

  3. 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',

  4. Then copy your PRIMARY KEY value from the portal and make it the value of the config.MASTERKEY in DocumentDBGetStarted.py. You've now updated your app with all the info it needs to communicate with Azure Cosmos DB.

    'MASTERKEY': 'FILLME',

Run the app

  1. In Visual Studio, right-click on the project in Solution Explorer, select the current Python environment, then right click.

  2. Select Install Python Package, then type in pydocumentdb

  3. 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.

Review SLAs in the Azure portal

[!INCLUDE cosmosdb-tutorial-review-slas]

Clean up resources

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:

  1. From the left-hand menu in the Azure portal, click Resource groups and then click the name of the resource you created.
  2. On your resource group page, click Delete, type the name of the resource to delete in the text box, and then click Delete.

Next steps

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