Quickstart: Build a Cassandra app with .NET and Azure Cosmos DB

This quickstart shows how to use .NET and the Azure Cosmos DB Cassandra API to build a profile app by cloning an example from GitHub. This quickstart also shows you how to use the web-based Azure portal to create an Azure Cosmos DB account.

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

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.

In addition, you need:

  • 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.
  • Install Git to clone the example.

Create a database account

  1. In a new browser window, sign in to the Azure portal.

  2. Select Create a resource > Databases > Azure Cosmos DB.

    The Azure portal Databases pane

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

    Setting Value Description
    Subscription Your subscription Select the 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 in ID
    Select Create new. Then enter a new resource-group name for your account. For simplicity, use the same name as your ID.
    Account Name Enter a unique name Enter a unique name to identify your Azure Cosmos DB account. Because documents.azure.com is appended to the ID that you provide to create your URI, use a unique ID.

    The ID can use only lowercase letters, numbers, and the hyphen (-) character. It must be between 3 and 31 characters in length.
    API Cassandra The API determines the type of account to create. Azure Cosmos DB provides five APIs: Core(SQL) for document databases, Gremlin for graph databases, MongoDB for document databases, Azure Table, and Cassandra. Currently, you must create a separate account for each API.

    Select Cassandra because in this quickstart you are creating a table that works with the Cassandra API.

    Learn more about the Cassandra API.
    Location Select the region closest to your users Select a geographic location 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.

    Select Review+Create. You can skip the Network and Tags section.

    The new account page for Azure Cosmos DB

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

Clone the sample application

Now let's switch to working with code. Let's clone a Cassandra API app from GitHub, set the connection string, and run it. You'll 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-dotnet-getting-started.git
    
  4. Next, open the CassandraQuickStartSample solution file in Visual Studio.

Review the code

This step is optional. If you're interested to learn how the code creates the database resources, you can review the following snippets. The snippets are all taken from the Program.cs file installed in the C:\git-samples\azure-cosmos-db-cassandra-dotnet-getting-started\CassandraQuickStartSample folder. Otherwise, you can skip ahead to Update your connection string.

  • Initialize the session by connecting to a Cassandra cluster endpoint. The Cassandra API on Azure Cosmos DB supports only TLSv1.2.

     var options = new Cassandra.SSLOptions(SslProtocols.Tls12, true, ValidateServerCertificate);
     options.SetHostNameResolver((ipAddress) => CassandraContactPoint);
     Cluster cluster = Cluster.Builder().WithCredentials(UserName, Password).WithPort(CassandraPort).AddContactPoint(CassandraContactPoint).WithSSL(options).Build();
     ISession session = cluster.Connect();
    
  • Create a new keyspace.

    session.Execute("CREATE KEYSPACE uprofile WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 1 };"); 
    
  • Create a new table.

    session.Execute("CREATE TABLE IF NOT EXISTS uprofile.user (user_id int PRIMARY KEY, user_name text, user_bcity text)");
    
  • Insert user entities by using the IMapper object with a new session that connects to the uprofile keyspace.

    mapper.Insert<User>(new User(1, "LyubovK", "Dubai"));
    
  • Query to get all user's information.

    foreach (User user in mapper.Fetch<User>("Select * from user"))
    {
      Console.WriteLine(user);
    }
    
  • Query to get a single user's information.

    mapper.FirstOrDefault<User>("Select * from user where user_id = ?", 3);
    

Update your connection string

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

  1. In the Azure portal, select Connection String.

    Use the Copy button button on the right side of the screen to copy the USERNAME value.

    View and copy an access key in the Azure portal, Connection String page

  2. In Visual Studio 2017, open the Program.cs file.

  3. Paste the USERNAME value from the portal over <FILLME> on line 13.

    Line 13 of Program.cs should now look similar to

    private const string UserName = "cosmos-db-quickstart";

  4. Go back to portal and copy the PASSWORD value. Paste the PASSWORD value from the portal over <FILLME> on line 14.

    Line 14 of Program.cs should now look similar to

    private const string Password = "2Ggkr662ifxz2Mg...==";

  5. Go back to portal and copy the CONTACT POINT value. Paste the CONTACT POINT value from the portal over <FILLME> on line 15.

    Line 15 of Program.cs should now look similar to

    private const string CassandraContactPoint = "cosmos-db-quickstarts.cassandra.cosmosdb.azure.com"; // DnsName

  6. Save the Program.cs file.

Run the .Net app

  1. In Visual Studio, select Tools > NuGet Package Manager > Package Manager Console.

  2. At the command prompt, use the following command to install the .NET Driver's NuGet package.

    Install-Package CassandraCSharpDriver
    
  3. Press CTRL + F5 to run the application. Your app displays in your console window.

    View and verify the output

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

  4. In the Azure portal, open Data Explorer to 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 a web app. You can now import additional data to your Cosmos DB account.