Skip to content

Latest commit

 

History

History
210 lines (134 loc) · 7.83 KB

create-cassandra-python.md

File metadata and controls

210 lines (134 loc) · 7.83 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
Quickstart: Cassandra API with Python - Azure Cosmos DB | Microsoft Docs
This quickstart shows how to use the Azure Cosmos DB's Apache Cassandra API to create a profile application with Python
cosmos-db
mimig1
jhubbard
4ebc883e-c512-4e34-bd10-19f048661159
cosmos-db
quick start connect, mvc
data-services
na
python
quickstart
11/15/2017
govindk

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.

Prerequisites

[!INCLUDE quickstarts-free-trial-note] 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.

[!INCLUDE cosmos-db-create-dbaccount-cassandra]

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 git terminal window, such as git bash, and use the cd command to change to a folder to install the sample app.

    cd "C:\git-samples"
  2. 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'))
    ....
    session.execute(batch)
  • Query to get 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.documents.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

[!INCLUDE cosmosdb-tutorial-review-slas]

Clean up resources

[!INCLUDE cosmosdb-delete-resource-group]

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 Cassandra data into Azure Cosmos DB