title | description | ms.service | ms.subservice | author | ms.author | ms.devlang | ms.topic | ms.date | ms.custom |
---|---|---|---|---|---|---|---|---|---|
Quickstart: Cassandra API with .NET Core - Azure Cosmos DB |
This quickstart shows how to use the Azure Cosmos DB Cassandra API to create a profile application with the Azure portal and .NET Core |
cosmos-db |
cosmosdb-cassandra |
TheovanKraay |
thvankra |
dotnet |
quickstart |
10/01/2020 |
devx-track-dotnet |
[!div class="op_single_selector"]
This quickstart shows how to use .NET Core 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.
[!INCLUDE quickstarts-free-trial-note] 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 2019 installed, you can download and use the free Visual Studio 2019 Community Edition. Make sure that you enable Azure development during the Visual Studio setup.
- Install Git to clone the example.
[!INCLUDE cosmos-db-create-dbaccount-cassandra]
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.
-
Open a command prompt. Create a new folder named
git-samples
. Then, close the command prompt.md "C:\git-samples"
-
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"
-
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-core-getting-started.git
-
Next, open the CassandraQuickStartSample solution file in Visual Studio.
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 within async Task ProcessAsync()
method, installed in the C:\git-samples\azure-cosmos-db-cassandra-dotnet-core-getting-started\CassandraQuickStart
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 = await cluster.ConnectAsync();
-
Drop existing keyspace if it already exists.
await session.ExecuteAsync(new SimpleStatement("DROP KEYSPACE IF EXISTS uprofile"));
-
Create a new keyspace.
await session.ExecuteAsync(new SimpleStatement("CREATE KEYSPACE uprofile WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 1 };"));
-
Create a new table.
await session.ExecuteAsync(new SimpleStatement("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.
await mapper.InsertAsync<User>(new User(1, "LyubovK", "Dubai"));
-
Query to get all user's information.
foreach (User user in await mapper.FetchAsync<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);
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.
-
In the Azure portal, select Connection String.
-
Use the :::image type="icon" source="./media/create-cassandra-dotnet/copy.png"::: button on the right side of the screen to copy the USERNAME value.
:::image type="content" source="./media/create-cassandra-dotnet/keys.png" alt-text="View and copy an access key in the Azure portal, Connection String page":::
-
In Visual Studio, open the Program.cs file.
-
Paste the USERNAME value from the portal over
<PROVIDE>
on line 13.Line 13 of Program.cs should now look similar to
private const string UserName = "cosmos-db-quickstart";
You can also paste the same value over
<PROVIDE>
on line 15 for the CONTACT POINT value:private const string CassandraContactPoint = "cosmos-db-quickstarts.cassandra.cosmosdb.azure.com"; // DnsName
-
Go back to portal and copy the PASSWORD value. Paste the PASSWORD value from the portal over
<PROVIDE>
on line 14.Line 14 of Program.cs should now look similar to
private const string Password = "2Ggkr662ifxz2Mg...==";
-
Go back to portal and copy the CONTACT POINT value. Paste the CONTACT POINT value from the portal over
<PROVIDE>
on line 16.Line 16 of Program.cs should now look similar to
private const string CASSANDRACONTACTPOINT = "quickstart-cassandra-api.cassandra.cosmos.azure.com";
-
Save the Program.cs file.
-
In Visual Studio, select Tools > NuGet Package Manager > Package Manager Console.
-
At the command prompt, use the following command to install the .NET Driver's NuGet package.
Install-Package CassandraCSharpDriver
-
Press CTRL + F5 to run the application. Your app displays in your console window.
:::image type="content" source="./media/create-cassandra-dotnet/output.png" alt-text="View and verify the output":::
Press CTRL + C to stop execution of the program and close the console window.
-
In the Azure portal, open Data Explorer to query, modify, and work with this new data.
:::image type="content" source="./media/create-cassandra-dotnet/data-explorer.png" alt-text="View the data in Data Explorer":::
[!INCLUDE cosmosdb-tutorial-review-slas]
[!INCLUDE cosmosdb-delete-resource-group]
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.
[!div class="nextstepaction"] Import Cassandra data into Azure Cosmos DB