Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

9/17/2020

Reading time:2 min

DataStax-Examples/switch-connection-csharp

by John Doe

This application shows how to use the C# DataStax Driver to connect to an on-prem Cassandra database or a Astra database in the cloud at runtime using environment variables.Contributor(s): Dave BechbergerObjectivesShows the differences between a Cassandra connection configuration and an Astra connection configuration in a single app.Provide a demonstration of how to configure the database connection at runtime.See the documentation for more details about the Astra connection configuration for the C# DriverProject LayoutProgram.cs - The main application file which contains the logic to switch between the configurationsHow this Sample WorksThis sample uses environment variables to specify the configuration parameters and whether to use a Cassandra (DSE/DDAC/C*) configuration or an Astra configuration.All the logic to switch between the configurations occurs in the GetClientConfiguration method.If you specify the USEASTRA environment variable and it is true:The environment variables are checked to see that DBUSERNAME, DBPASSWORD, SECURECONNECTBUNDLEPATH, and KEYSPACE existIf they exist then the Astra connection is createdIf they do not exist then an error is thrownIf you so not specify the USEASTRA environment variable or it is false:The environment variables are checked to see that CONTACTPOINTS and DATACENTER existIf they exist then the standard connection is createdIf they do not exist then an error is thrownIf DBUSERNAME and DBPASSWORD exist then credentials are added to the configurationIf KEYSPACE exists then it is added to the configurationWhile these criteria added to the configuration are commonly used configuration parameters you are able to specify any additional ones in the code.Additionally while this example uses environment variables to control which configuration is selected this could also be done via the use of configuration files or command line parameters.For clarity this sample does not contain any of the normal error handling process you would want to wrap around connecting to a cluster to handle likely errors that would occur.Setup and RunningPrerequisites.NET Core 2.1A Cassandra cluster or an Astra database to connect to with the appropriate connection informationRunningTo connect to an Astra database you first need to download the secure connect bundle following the instructions found here.This first step in the process is to build the application. This can be done using the following command:dotnet buildOnce you have compiled the connection information and built the application you can run this to connect to Astra by using the command below, with the appropriate configuration added:USEASTRA=true DBUSERNAME=XXX DBPASSWORD=XXX KEYSPACE=XXX SECURECONNECTBUNDLEPATH="/valid/path/to/secureconnectbundle.zip" dotnet runIf you would like to connect to a Cassandra cluster use the command below, with the appropriate configuration added:CONTACTPOINTS=XX.XX.XX.XX dotnet runOnce you run this against either option you will get output that specifies the number of hosts and their IP addresses printed to the console:Connected to cluster with 3 host(s) XX.XX.XX.XX:9042,XX.XX.XX.XX:9042,XX.XX.XX.XX:9042

Illustration Image

This application shows how to use the C# DataStax Driver to connect to an on-prem Cassandra database or a Astra database in the cloud at runtime using environment variables.

Contributor(s): Dave Bechberger

Objectives

  • Shows the differences between a Cassandra connection configuration and an Astra connection configuration in a single app.
  • Provide a demonstration of how to configure the database connection at runtime.
  • See the documentation for more details about the Astra connection configuration for the C# Driver

Project Layout

  • Program.cs - The main application file which contains the logic to switch between the configurations

How this Sample Works

This sample uses environment variables to specify the configuration parameters and whether to use a Cassandra (DSE/DDAC/C*) configuration or an Astra configuration.
All the logic to switch between the configurations occurs in the GetClientConfiguration method.

  • If you specify the USEASTRA environment variable and it is true:
    • The environment variables are checked to see that DBUSERNAME, DBPASSWORD, SECURECONNECTBUNDLEPATH, and KEYSPACE exist
      • If they exist then the Astra connection is created
      • If they do not exist then an error is thrown
  • If you so not specify the USEASTRA environment variable or it is false:
    • The environment variables are checked to see that CONTACTPOINTS and DATACENTER exist
      • If they exist then the standard connection is created
      • If they do not exist then an error is thrown
      • If DBUSERNAME and DBPASSWORD exist then credentials are added to the configuration
      • If KEYSPACE exists then it is added to the configuration

While these criteria added to the configuration are commonly used configuration parameters you are able to specify any additional ones in the code.

Additionally while this example uses environment variables to control which configuration is selected this could also be done via the use of configuration files or command line parameters.

For clarity this sample does not contain any of the normal error handling process you would want to wrap around connecting to a cluster to handle likely errors that would occur.

Setup and Running

Prerequisites

  • .NET Core 2.1
  • A Cassandra cluster or an Astra database to connect to with the appropriate connection information

Running

To connect to an Astra database you first need to download the secure connect bundle following the instructions found here. This first step in the process is to build the application. This can be done using the following command:

dotnet build

Once you have compiled the connection information and built the application you can run this to connect to Astra by using the command below, with the appropriate configuration added:

USEASTRA=true DBUSERNAME=XXX DBPASSWORD=XXX KEYSPACE=XXX SECURECONNECTBUNDLEPATH="/valid/path/to/secureconnectbundle.zip" dotnet run

If you would like to connect to a Cassandra cluster use the command below, with the appropriate configuration added:

CONTACTPOINTS=XX.XX.XX.XX dotnet run

Once you run this against either option you will get output that specifies the number of hosts and their IP addresses printed to the console:

Connected to cluster with 3 host(s) XX.XX.XX.XX:9042,XX.XX.XX.XX:9042,XX.XX.XX.XX:9042

Related Articles

cluster
troubleshooting
datastax

GitHub - arodrime/Montecristo: Datastax Cluster Health Check Tooling

arodrime

4/3/2024

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

csharp