Quickstart: Build a Cassandra app with Python and Azure Cosmos DB

This quickstart shows how to use Python and the Azure Cosmos DB Cassandra API to build a profile app by cloning an example from GitHub. This quickstart also walks you through the creation of an Azure Cosmos DB account by using the web-based Azure portal.

Azure Cosmos DB is Microsoft's globally distributed multi-model database service. You can quickly create and query document, table, key-value, and graph databases, all of which benefit from the global distribution and horizontal scale capabilities at the core of Azure Cosmos DB.


If you don't have an Azure subscription, create a free account before you begin. Alternatively, you can Try Azure Cosmos DB for free without an Azure subscription, free of charge and commitments.

Access to the Azure Cosmos DB Cassandra API preview program. If you haven't applied for access yet, sign up now.

In addition:

Create a database account

Before you can create a document database, you need to create a Cassandra account with Azure Cosmos DB.

  1. In a new browser window, sign in to the Azure portal.
  2. Click Create a resource > Databases > Azure Cosmos DB.

    The Azure portal Databases pane

  3. In the New account page, enter the settings for the new Azure Cosmos DB account.

    Setting Suggested value Description
    ID Enter a unique name Enter a unique name to identify this Azure Cosmos DB account. Because cassandra.cosmosdb.azure.com is appended to the ID that you provide to create your contact point, use a unique but identifiable ID.

    The ID can contain only lowercase letters, numbers, and the hyphen (-) character, and it must contain 3 to 50 characters.
    API Cassandra The API determines the type of account to create. Azure Cosmos DB provides five APIs to suits the needs of your application: SQL (document database), Gremlin (graph database), MongoDB (document database), Azure Table, and Cassandra, each which currently require a separate account.

    Select Cassandra because in this quickstart you are creating a wide-column database that is queryable using CQL syntax.

    If Cassandra (wide-column) is not displayed in your list, then you need to apply to join the Cassandra API preview program.

    Learn more about the Cassandra API
    Subscription Your subscription Select Azure subscription that you want to use for this Azure Cosmos DB account.
    Resource Group Create new

    Then enter the same unique name as provided above in ID
    Select Create New, then enter a new resource-group name for your account. For simplicity, you can use the same name as your ID.
    Location Select the region closest to your users Select geographic location in which to host your Azure Cosmos DB account. Use the location that's closest to your users to give them the fastest access to the data.
    Pin to dashboard Select Select this box so that your new database account is added to your portal dashboard for easy access.

    Then click Create.

    The new account page for Azure Cosmos DB

  4. The account creation takes a few minutes. Wait for the portal to display the Congratulations! Your Azure Cosmos DB account was created page.

Clone the sample application

Now let's clone a Cassandra 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 command prompt, create a new folder named git-samples, then close the command prompt.

    md "C:\git-samples"
  2. Open a git terminal window, such as git bash, and use the cd command to change to the new folder to install the sample app.

    cd "C:\git-samples"
  3. Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.

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

Review the code

This step is optional. If you're interested in learning how the database resources are created in the code, you can review the following snippets. The snippets are all taken from the pyquickstart.py file. Otherwise, you can skip ahead to Update your connection string.

  • User name and password is set using the connection string page in the Azure portal. You replace the path\to\cert with the path to your X509 certificate.

     ssl_opts = {
             'ca_certs': 'path\to\cert',
             'ssl_version': ssl.PROTOCOL_TLSv1_2
     auth_provider = PlainTextAuthProvider( username=cfg.config['username'], password=cfg.config['password'])
     cluster = Cluster([cfg.config['contactPoint']], port = cfg.config['port'], auth_provider=auth_provider, ssl_options=ssl_opts)
     session = cluster.connect()
  • The cluster is initialized with contactPoint information. The contactPoint is retrieved from the Azure portal.

    cluster = Cluster([cfg.config['contactPoint']], port = cfg.config['port'], auth_provider=auth_provider)
  • The cluster connects to the Azure Cosmos DB Cassandra API.

    session = cluster.connect()
  • A new keyspace is created.

    session.execute('CREATE KEYSPACE IF NOT EXISTS uprofile WITH replication = {\'class\': \'NetworkTopologyStrategy\', \'datacenter1\' : \'1\' }')
  • A new table is created.

    session.execute('CREATE TABLE IF NOT EXISTS uprofile.user (user_id int PRIMARY KEY, user_name text, user_bcity text)');
  • Key/value entities are inserted.

    insert_data = session.prepare("INSERT INTO  uprofile.user  (user_id, user_name , user_bcity) VALUES (?,?,?)")
    batch = BatchStatement()
    batch.add(insert_data, (1, 'LyubovK', 'Dubai'))
    batch.add(insert_data, (2, 'JiriK', 'Toronto'))
    batch.add(insert_data, (3, 'IvanH', 'Mumbai'))
    batch.add(insert_data, (4, 'YuliaT', 'Seattle'))
  • Query to get all key values.

    rows = session.execute('SELECT * FROM uprofile.user')
  • Query to get a key-value.

    rows = session.execute('SELECT * FROM uprofile.user where user_id=1')

Update your connection string

Now go back to the Azure portal to get your connection string information and copy it into the app. This enables your app to communicate with your hosted database.

  1. In the Azure portal, click Connection String.

    Use the Copy button button on the right side of the screen to copy the top value, the CONTACT POINT.

    View and copy an access user name, password and contact point in the Azure portal, connection string blade

  2. Open the config.py file.

  3. Paste the CONTACT POINT value from the portal over <FILLME> on line 10.

    Line 10 should now look similar to

    'contactPoint': 'cosmos-db-quickstarts.cassandra.cosmosdb.azure.com:10350'

  4. Copy the USERNAME value from the portal and paste it over <FILLME> on line 6.

    Line 6 should now look similar to

    'username': 'cosmos-db-quickstart',

  5. Copy the PASSWORD value from the portal and paste it over <FILLME> on line 8.

    Line 8 should now look similar to

    'password' = '2Ggkr662ifxz2Mg==';`

  6. Save the config.py file.

Use the X509 certificate

  1. If you need to add the Baltimore CyberTrust Root, it has serial number 02:00:00:b9 and SHA1 fingerprint d4🇩🇪20:d0:5e:66:fc:53:fe:1a:50:88:2c:78:db:28:52:ca:e4:74. It can be downloaded from https://cacert.omniroot.com/bc2025.crt, saved to a local file with extension .cer

  2. Open pyquickstart.py and change the 'path\to\cert' to point to your new certificate.

  3. Save pyquickstart.py.

Run the app

  1. Use the cd command in the git terminal to change into the azure-cosmos-db-cassandra-python-getting-started folder.

  2. Run the following commands to install the required modules:

    python -m pip install cassandra-driver
    python -m pip install prettytable
    python -m pip install requests
    python -m pip install pyopenssl
  3. Run the following command to start your node application:

    python pyquickstart.py
  4. Verify the results as expected from the command line.

    Press CTRL + C to stop exection of the program and close the console window.

    View and verify the output

    You can now open Data Explorer in the Azure portal to see query, modify, and work with this new data.

    View the data in Data Explorer

Review SLAs in the Azure portal

The throughput, storage, availability, latency, and consistency of the resources in your account are monitored in the Azure portal. Let's take a quick look at these metrics.

  1. Click Metrics in the navigation menu.

    Metrics in the Azure portal

  2. Click through each of the tabs so you're aware of the metrics Azure Cosmos DB provides.

    Each chart that's associated with the Azure Cosmos DB Service Level Agreements (SLAs) provides a line that shows if any of the SLAs have been violated. Azure Cosmos DB makes monitoring your SLAs transparent with this suite of metrics.

    Azure Cosmos DB metrics suite

Clean up resources

If you're not going to continue to use this app, delete all resources created by this quickstart with the following steps so you don't incur any charges:

  1. In the Azure portal, select Resource groups on the far left, and then select the resource group you created.

    If the left menu is collapsed, click Expand button to expand it.

    Metrics in the Azure portal

  2. In the new window select the resource group, and then click Delete resource group.

    Metrics in the Azure portal

  3. In the new window, type the name of the resource group to delete, and then click Delete.

Next steps

In this quickstart, you've learned how to create an Azure Cosmos DB account, create a container using the Data Explorer, and run an app. You can now import additional data to your Cosmos DB account.