Using a Cassandra .NET Core Client Driver to Access Amazon Keyspaces Programmatically - Amazon Keyspaces (for Apache Cassandra)

Using a Cassandra .NET Core Client Driver to Access Amazon Keyspaces Programmatically

This section shows you how to connect to Amazon Keyspaces by using a .NET Core client driver. The setup steps will vary depending on your environment and operating system, you might have to modify them accordingly. Amazon Keyspaces requires the use of Transport Layer Security (TLS) to help secure connections with clients. To connect to Amazon Keyspaces using TLS, you need to download an Amazon digital certificate and configure your driver to use TLS.

  1. Download the Amazon certificate and save it to a local directory, taking note of the path. Following is an example using PowerShell.

    $client = new-object System.Net.WebClient $client.DownloadFile("","path_to_file\AmazonRootCA1.pem")
  2. Install the CassandraCSharpDriver through nuget, using the nuget console.

    PM> Install-Package CassandraCSharpDriver
  3. The following example uses a .NET Core C# console project to connect to &MCS; and execute a query.

    using Cassandra; using System; using System.Collections.Generic; using System.Linq; using System.Net.Security; using System.Runtime.ConstrainedExecution; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; namespace CSharpKeyspacesExample { class Program { public Program(){} static void Main(string[] args) { X509Certificate2Collection certCollection = new X509Certificate2Collection(); X509Certificate2 amazoncert = new X509Certificate2(@"path_to_file\AmazonRootCA1.pem"); var userName = "ServiceUserName"; var pwd = "ServicePassword"; certCollection.Add(amazoncert); var awsEndpoint = "" ; var cluster = Cluster.Builder() .AddContactPoints(awsEndpoint) .WithPort(9142) .WithAuthProvider(new PlainTextAuthProvider(userName, pwd)) .WithSSL(new SSLOptions().SetCertificateCollection(certCollection)) .Build(); var session = cluster.Connect(); var rs = session.Execute("SELECT * FROM system_schema.tables;"); foreach (var row in rs) { var name = row.GetValue<String>("keyspace_name"); Console.WriteLine(name); } } } }

    Usage notes:

    1. Replace "path_to_file/AmazonRootCA1.pem" with the path to the certificate saved in the first step.

    2. Ensure that the ServiceUserName and ServicePassword match the user name and password you obtained when you generated the service-specific credentials by following the steps to Generate Service-Specific Credentials.

    3. For a list of available endpoints, see Service Endpoints for Amazon Keyspaces.