使用 Cassandra .NET Core 客户端驱动程序以编程方式访问 Amazon Keyspaces - Amazon Keyspaces (for Apache Cassandra)
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Cassandra .NET Core 客户端驱动程序以编程方式访问 Amazon Keyspaces

本节说明了如何使用.NET Core 客户端驱动程序连接到 Amazon Keyspaces。安装步骤将因您的环境和操作系统而异,您可能需要相应地对其进行修改。Amazon Keyspaces 要求使用传输层安全 (TLS) 来帮助保护与客户端的连接。要使用 TLS 连接到 Amazon Keyspaces,您需要下载 Starfield 数字证书并将您的驱动程序配置为使用 TLS。

  1. 下载 Starfield 证书并将其保存到本地目录中,记下路径。以下是使用的示例 PowerShell。

    $client = new-object System.Net.WebClient $client.DownloadFile("https://certs.secureserver.net/repository/sf-class2-root.crt","path_to_file\sf-class2-root.crt")
  2. 使用 nuget 控制台SharpDriver 通过 nuget 安装 CassanDrac。

    PM> Install-Package CassandraCSharpDriver
  3. 以下示例使用.NET Core C# 控制台项目连接到 &MCS; 并运行查询。

    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\sf-class2-root.crt"); var userName = "ServiceUserName"; var pwd = "ServicePassword"; certCollection.Add(amazoncert); var awsEndpoint = "cassandra.us-east-2.amazonaws.com" ; 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); } } } }

    使用说明:

    1. "path_to_file/sf-class2-root.crt"替换为第一步中保存的证书路径。

    2. 按照中的步骤,确保ServiceUserNameServicePassword与您在生成服务特定凭证时获得的用户名和密码相匹配生成特定服务的凭证

    3. 有关可用终端节点的列表,请参阅Amazon Keyspaces 服务端点