本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Cassandra 客户端驱动程序以编程方式访问 Amazon Keyspaces
本部分介绍了如何使用 Java 客户端驱动程序连接 Amazon Keyspaces。
注意
Java 17 和 DataStax Java 驱动程序 4.17 目前仅支持测试版。有关更多信息,请参阅 https://docs.datastax.com/en/developer/java-driver/4.17/upgrade_guide/
要为用户和应用程序提供凭证,以通过编程方式访问 Amazon Keyspaces 资源,您可以执行以下任一操作:
-
创建与特定 AWS Identity and Access Management (IAM) 用户关联的服务特定凭证。
-
为了增强安全性,我们建议为所有 AWS 服务中使用的 IAM 身份创建 IAM 访问密钥。借助适用于 Cassandra 客户端驱动程序的 Amazon Keyspaces SigV4 身份验证插件,您可以使用 IAM 访问密钥而不是用户名和密码来验证对 Amazon Keyspaces 的调用。有关更多信息,请参阅 为 Amazon Keyspaces 创建和配置 AWS 证书。
注意
有关如何在 Spring Boot 中使用 Amazon Keyspaces 的示例,请参阅。https://github.com/aws-samples/amazon-keyspaces-examples/tree/main/java/datastax-v4/spring
主题
开始前的准备工作
要连接到 Amazon Keyspaces,您需要先完成以下任务。
Amazon Keyspaces 要求使用传输层安全性协议 (TLS) 来帮助保护与客户端的连接。
下载以下数字证书并将文件保存在本地或您的主目录中。
AmazonRootCA1
AmazonRootCA2
AmazonRootCA3
AmazonRootCA4
Starfield Class 2 Root(可选 — 为了向后兼容)
要下载证书,您可以使用以下命令。
curl -O https://www.amazontrust.com/repository/AmazonRootCA1.pem curl -O https://www.amazontrust.com/repository/AmazonRootCA2.pem curl -O https://www.amazontrust.com/repository/AmazonRootCA3.pem curl -O https://www.amazontrust.com/repository/AmazonRootCA4.pem curl -O https://certs.secureserver.net/repository/sf-class2-root.crt注意
Amazon Keyspaces 之前使用锚定在 Starfield Class 2 CA 上的 TLS 证书。 AWS 正在将所有证书全部迁移 AWS 区域 到根据亚马逊信任服务(Amazon Root CAs 1—4)颁发的证书。在此过渡期间,将客户端配置为同时信任 Amazon Root CAs 1—4 和 Starfield 根,以确保所有区域之间的兼容性。
将数字证书转换为 TrustStore 文件并将其添加到密钥库中。
openssl x509 -outform der -in AmazonRootCA1.pem -out temp_file.der keytool -import -alias amazon-root-ca-1 -keystore cassandra_truststore.jks -file temp_file.der openssl x509 -outform der -in AmazonRootCA2.pem -out temp_file.der keytool -import -alias amazon-root-ca-2 -keystore cassandra_truststore.jks -file temp_file.der openssl x509 -outform der -in AmazonRootCA3.pem -out temp_file.der keytool -import -alias amazon-root-ca-3 -keystore cassandra_truststore.jks -file temp_file.der openssl x509 -outform der -in AmazonRootCA4.pem -out temp_file.der keytool -import -alias amazon-root-ca-4 -keystore cassandra_truststore.jks -file temp_file.der openssl x509 -outform der -in sf-class2-root.crt -out temp_file.der keytool -import -alias cassandra -keystore cassandra_truststore.jks -file temp_file.der在最后一步中,您需要为密钥库创建密码并信任每个证书。交互式命令如下所示。
Enter keystore password: Re-enter new password: Owner: CN=Amazon Root CA 1, O=Amazon, C=US Issuer: CN=Amazon Root CA 1, O=Amazon, C=US Serial number: 66c9fcf99bf8c0a39e2f0788a43e696365bca Valid from: Tue May 26 00:00:00 UTC 2015 until: Sun Jan 17 00:00:00 UTC 2038 Certificate fingerprints: SHA1: 8D:A7:F9:65:EC:5E:FC:37:91:0F:1C:6E:59:FD:C1:CC:6A:6E:DE:16 SHA256: 8E:CD:E6:88:4F:3D:87:B1:12:5B:A3:1A:C3:FC:B1:3D:70:16:DE:7F:57:CC:90:4F:E1:CB:97:C6:AE:98:19:6E Signature algorithm name: SHA256withRSA Subject Public Key Algorithm: 2048-bit RSA key Version: 3 Extensions: #1: ObjectId: 2.5.29.19 Criticality=true BasicConstraints:[ CA:true PathLen:2147483647 ] #2: ObjectId: 2.5.29.15 Criticality=true KeyUsage [ DigitalSignature Key_CertSign Crl_Sign ] #3: ObjectId: 2.5.29.14 Criticality=false SubjectKeyIdentifier [ KeyIdentifier [ 0000: 84 18 CC 85 34 EC BC 0C 94 94 2E 08 59 9C C7 B2 ....4.......Y... 0010: 10 4E 0A 08 .N.. ] ] Trust this certificate? [no]: yes Certificate was added to keystore Enter keystore password: Owner: CN=Amazon Root CA 2, O=Amazon, C=US Issuer: CN=Amazon Root CA 2, O=Amazon, C=US Serial number: 66c9fd29635869f0a0fe58678f85b26bb8a37 Valid from: Tue May 26 00:00:00 UTC 2015 until: Sat May 26 00:00:00 UTC 2040 Certificate fingerprints: SHA1: 5A:8C:EF:45:D7:A6:98:59:76:7A:8C:8B:44:96:B5:78:CF:47:4B:1A SHA256: 1B:A5:B2:AA:8C:65:40:1A:82:96:01:18:F8:0B:EC:4F:62:30:4D:83:CE:C4:71:3A:19:C3:9C:01:1E:A4:6D:B4 Signature algorithm name: SHA384withRSA Subject Public Key Algorithm: 4096-bit RSA key Version: 3 Extensions: #1: ObjectId: 2.5.29.19 Criticality=true BasicConstraints:[ CA:true PathLen:2147483647 ] #2: ObjectId: 2.5.29.15 Criticality=true KeyUsage [ DigitalSignature Key_CertSign Crl_Sign ] #3: ObjectId: 2.5.29.14 Criticality=false SubjectKeyIdentifier [ KeyIdentifier [ 0000: B0 0C F0 4C 30 F4 05 58 02 48 FD 33 E5 52 AF 4B ...L0..X.H.3.R.K 0010: 84 E3 66 52 ..fR ] ] Trust this certificate? [no]: yes Certificate was added to keystore Enter keystore password: Owner: CN=Amazon Root CA 3, O=Amazon, C=US Issuer: CN=Amazon Root CA 3, O=Amazon, C=US Serial number: 66c9fd5749736663f3b0b9ad9e89e7603f24a Valid from: Tue May 26 00:00:00 UTC 2015 until: Sat May 26 00:00:00 UTC 2040 Certificate fingerprints: SHA1: 0D:44:DD:8C:3C:8C:1A:1A:58:75:64:81:E9:0F:2E:2A:FF:B3:D2:6E SHA256: 18:CE:6C:FE:7B:F1:4E:60:B2:E3:47:B8:DF:E8:68:CB:31:D0:2E:BB:3A:DA:27:15:69:F5:03:43:B4:6D:B3:A4 Signature algorithm name: SHA256withECDSA Subject Public Key Algorithm: 256-bit EC (secp256r1) key Version: 3 Extensions: #1: ObjectId: 2.5.29.19 Criticality=true BasicConstraints:[ CA:true PathLen:2147483647 ] #2: ObjectId: 2.5.29.15 Criticality=true KeyUsage [ DigitalSignature Key_CertSign Crl_Sign ] #3: ObjectId: 2.5.29.14 Criticality=false SubjectKeyIdentifier [ KeyIdentifier [ 0000: AB B6 DB D7 06 9E 37 AC 30 86 07 91 70 C7 9C C4 ......7.0...p... 0010: 19 B1 78 C0 ..x. ] ] Trust this certificate? [no]: yes Certificate was added to keystore Enter keystore password: Owner: CN=Amazon Root CA 4, O=Amazon, C=US Issuer: CN=Amazon Root CA 4, O=Amazon, C=US Serial number: 66c9fd7c1bb104c2943e5717b7b2cc81ac10e Valid from: Tue May 26 00:00:00 UTC 2015 until: Sat May 26 00:00:00 UTC 2040 Certificate fingerprints: SHA1: F6:10:84:07:D6:F8:BB:67:98:0C:C2:E2:44:C2:EB:AE:1C:EF:63:BE SHA256: E3:5D:28:41:9E:D0:20:25:CF:A6:90:38:CD:62:39:62:45:8D:A5:C6:95:FB:DE:A3:C2:2B:0B:FB:25:89:70:92 Signature algorithm name: SHA384withECDSA Subject Public Key Algorithm: 384-bit EC (secp384r1) key Version: 3 Extensions: #1: ObjectId: 2.5.29.19 Criticality=true BasicConstraints:[ CA:true PathLen:2147483647 ] #2: ObjectId: 2.5.29.15 Criticality=true KeyUsage [ DigitalSignature Key_CertSign Crl_Sign ] #3: ObjectId: 2.5.29.14 Criticality=false SubjectKeyIdentifier [ KeyIdentifier [ 0000: D3 EC C7 3A 65 6E CC E1 DA 76 9A 56 FB 9C F3 86 ...:en...v.V.... 0010: 6D 57 E5 81 mW.. ] ] Trust this certificate? [no]: yes Certificate was added to keystore Enter keystore password: Owner: OU=Starfield Class 2 Certification Authority, O="Starfield Technologies, Inc.", C=US Issuer: OU=Starfield Class 2 Certification Authority, O="Starfield Technologies, Inc.", C=US Serial number: 0 Valid from: Tue Jun 29 17:39:16 UTC 2004 until: Thu Jun 29 17:39:16 UTC 2034 Certificate fingerprints: SHA1: AD:7E:1C:28:B0:64:EF:8F:60:03:40:20:14:C3:D0:E3:37:0E:B5:8A SHA256: 14:65:FA:20:53:97:B8:76:FA:A6:F0:A9:95:8E:55:90:E4:0F:CC:7F:AA:4F:B7:C2:C8:67:75:21:FB:5F:B6:58 Signature algorithm name: SHA1withRSA (weak) Subject Public Key Algorithm: 2048-bit RSA key Version: 3 Extensions: #1: ObjectId: 2.5.29.35 Criticality=false AuthorityKeyIdentifier [ KeyIdentifier [ 0000: BF 5F B7 D1 CE DD 1F 86 F4 5B 55 AC DC D7 10 C2 ._.......[U..... 0010: 0E A9 88 E7 .... ] [OU=Starfield Class 2 Certification Authority, O="Starfield Technologies, Inc.", C=US] SerialNumber: [ 00] ] #2: ObjectId: 2.5.29.19 Criticality=false BasicConstraints:[ CA:true PathLen:2147483647 ] #3: ObjectId: 2.5.29.14 Criticality=false SubjectKeyIdentifier [ KeyIdentifier [ 0000: BF 5F B7 D1 CE DD 1F 86 F4 5B 55 AC DC D7 10 C2 ._.......[U..... 0010: 0E A9 88 E7 .... ] ] Warning: The input uses the SHA1withRSA signature algorithm which is considered a security risk. This algorithm will be disabled in a future update. Trust this certificate? [no]: yes Certificate was added to keystore
-
将 trustStore 文件附加到 JVM 参数中:
-Djavax.net.ssl.trustStore=path_to_file/cassandra_truststore.jks -Djavax.net.ssl.trustStorePassword=my_password
Step-by-step 使用适用于 Apache Cassandra 的 DataStax Java 驱动程序使用特定于服务的凭证连接亚马逊密钥空间的教程
以下 step-by-step教程将引导您使用特定于服务的凭证使用适用于 Cassandra 的 Java 驱动程序连接到 Amazon Keyspaces。具体而言,你将使用适用于 Apache Cassandr DataStax a 的 Java 驱动程序的 4.0 版本。
步骤 1:先决条件
要学习本教程,你需要生成特定于服务的凭证,并将适用于 Apache Cassandra 的 DataStax Java 驱动程序添加到你的 Java 项目中。
完成创建用于通过编程方式访问 Amazon Keyspaces 的服务特定凭证。中的步骤,为您的 Amazon Keyspaces IAM 用户生成服务特定凭证。如果您更喜欢使用 IAM 访问密钥进行身份验证,请参阅Step-by-step 使用适用于 Apache Cassandra 的 4.x DataStax Java 驱动程序和 Sigv4 身份验证插件连接亚马逊密钥空间的教程。
将 Apache Cassandra 的 DataStax Java 驱动程序添加到你的 Java 项目中。确保您使用的驱动程序版本支持 Apache Cassandra 3.11.2。有关更多信息,请参阅 A pache Cassandra 的 DataStax Java 驱动程序
文档。
步骤 2:配置驱动程序
您可以通过为应用程序创建配置文件来指定 DataStax Java Cassandra 驱动程序的设置。此配置文件会覆盖默认设置,并告诉驱动程序使用端口 9142 连接到 Amazon Keyspaces 服务端点。有关可用服务端点的列表,请参阅 Amazon Keyspaces 的服务端点。
创建配置文件,并将该文件保存在应用程序的资源文件夹中,例如 src/main/resources/application.conf。打开 application.conf,然后添加以下配置设置。
身份验证提供商-使用
PlainTextAuthProvider类创建身份验证提供程序。ServiceUserName并ServicePassword应与您按照中的步骤生成服务专用凭证时获得的用户名和密码相匹配。创建用于通过编程方式访问 Amazon Keyspaces 的服务特定凭证。注意
您可以使用适用于 Apache Cassandra 的 DataStax Java 驱动程序的身份验证插件,而不是在驱动程序配置文件中对凭据进行硬编码,从而使用短期证书。要了解更多信息,请按照Step-by-step 使用适用于 Apache Cassandra 的 4.x DataStax Java 驱动程序和 Sigv4 身份验证插件连接亚马逊密钥空间的教程中的说明进行操作。
本地数据中心:将
local-datacenter的值设置为要连接的区域。例如,如果应用程序要连接到cassandra.us-east-2.amazonaws.com,则将本地数据中心设置为us-east-2。有关所有可用的 AWS 区域,请参阅 Amazon Keyspaces 的服务端点。设置 slow-replica-avoidance = false 以针对更少的节点进行负载均衡。SSL/TLS — 通过在配置文件中添加一个部分来初始化 SSLEngine工厂,其中有一行用于指定类。
class = DefaultSslEngineFactory提供 trustStore 文件的路径和您之前创建的密码。Amazon Keyspaces 不支持对等设备的hostname-validation,因此请将此选项设为 false。
datastax-java-driver { basic.contact-points = [ "cassandra.us-east-2.amazonaws.com:9142"] advanced.auth-provider{ class = PlainTextAuthProvider username ="ServiceUserName"password ="ServicePassword"} basic.load-balancing-policy { local-datacenter ="us-east-2"slow-replica-avoidance = false } advanced.ssl-engine-factory { class = DefaultSslEngineFactory truststore-path ="./src/main/resources/cassandra_truststore.jks"truststore-password ="my_password"hostname-validation = false } }
注意
除了在配置文件中添加 trustStore 的路径外,您还可以直接在应用程序代码中添加 trustStore 的路径,或者在 JVM 参数中添加 trustStore 的路径。
步骤 3:运行示例应用程序
本代码示例展示了一个简单的命令行应用程序,它使用我们之前创建的配置文件创建了一个连接到 Amazon Keyspaces 的连接池。它通过运行一个简单的查询来确认连接已建立。
package<your package>; // add the following imports to your project import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.config.DriverConfigLoader; import com.datastax.oss.driver.api.core.cql.ResultSet; import com.datastax.oss.driver.api.core.cql.Row; public class App { public static void main( String[] args ) { //Use DriverConfigLoader to load your configuration file DriverConfigLoader loader = DriverConfigLoader.fromClasspath("application.conf"); try (CqlSession session = CqlSession.builder() .withConfigLoader(loader) .build()) { ResultSet rs = session.execute("select * from system_schema.keyspaces"); Row row = rs.one(); System.out.println(row.getString("keyspace_name")); } } }
注意
使用 try 代码块建立连接,以确保连接始终处于关闭状态。如果不使用 try 代码块,请记得关闭连接,以免泄漏资源。
Step-by-step 使用适用于 Apache Cassandra 的 4.x DataStax Java 驱动程序和 Sigv4 身份验证插件连接亚马逊密钥空间的教程
以下部分介绍如何使用适用于 Apache Cassandra 的开源 4.x DataStax Java 驱动程序的 SigV4 身份验证插件访问亚马逊密钥空间(适用于 Apache Cassandra)。该插件可从GitHub存储库
使用 SigV4 身份验证插件,您可以在连接 Amazon Keyspaces 时为用户或角色使用 IAM 凭据。该插件不需要用户名和密码,而是使用访问密钥签署 API 请求。有关更多信息,请参阅 为 Amazon Keyspaces 创建和配置 AWS 证书。
步骤 1:先决条件
要学习本教程,您需要完成以下任务。
请按照为 Amazon Keyspaces 创建和配置 AWS 证书中的步骤为 IAM 用户或角色创建凭证(如果您尚未创建)。本教程假设访问密钥将存储为环境变量。有关更多信息,请参阅 存储用于通过编程方式进行访问的访问密钥。
将 Apache Cassandra 的 DataStax Java 驱动程序添加到你的 Java 项目中。确保您使用的驱动程序版本支持 Apache Cassandra 3.11.2。有关更多信息,请参阅 A pache Cassandra 的 DataStax Java 驱动程序
文档。 将身份验证插件添加到您的应用程序。该身份验证插件支持 Apache Cassandr DataStax a 的 Java 驱动程序版本 4.x。如果您使用的是 Apache Maven 或可以使用 Maven 依赖关系的构建系统,请将以下依赖关系添加到您的
pom.xml文件中。重要
将插件版本替换为GitHub 存储库
中显示的最新版本。 <dependency> <groupId>software.aws.mcs</groupId> <artifactId>aws-sigv4-auth-cassandra-java-driver-plugin</artifactId> <version>4.0.9</version> </dependency>
步骤 2:配置驱动程序
您可以通过为应用程序创建配置文件来指定 DataStax Java Cassandra 驱动程序的设置。此配置文件会覆盖默认设置,并告诉驱动程序使用端口 9142 连接到 Amazon Keyspaces 服务端点。有关可用服务端点的列表,请参阅 Amazon Keyspaces 的服务端点。
创建配置文件,并将该文件保存在应用程序的资源文件夹中,例如 src/main/resources/application.conf。打开 application.conf,然后添加以下配置设置。
身份验证提供程序:将
advanced.auth-provider.class设置为software.aws.mcs.auth.SigV4AuthProvider的新实例。Sigv4 AuthProvider 是插件提供的用于执行 Sigv4 身份验证的身份验证处理程序。本地数据中心:将
local-datacenter的值设置为要连接的区域。例如,如果应用程序要连接到cassandra.us-east-2.amazonaws.com,则将本地数据中心设置为us-east-2。有关所有可用信息 AWS 区域,请参阅Amazon Keyspaces 的服务端点。设置slow-replica-avoidance = false以针对所有可用节点进行负载均衡。Idempotence — 将应用程序的默认值设置为,将驱动程序配置
idempotencetrue为始终重试失败的请求。read/write/prepare/execute这是适用于分布式应用程序的最佳实践,可通过重试失败的请求来协助处理暂时性故障。SSL/TLS — 通过在配置文件中添加一个部分来初始化 SSLEngine工厂,其中有一行用于指定类。
class = DefaultSslEngineFactory提供 trustStore 文件的路径和您之前创建的密码。Amazon Keyspaces 不支持对等设备的hostname-validation,因此请将此选项设为 false。连接 - 通过设置
local.size = 3为每个端点创建至少 3 个本地连接。这是一种最佳实践,方便您的应用程序应对开销和流量暴增。有关如何根据预期流量模式计算应用程序每个端点需要多少本地连接的更多信息,请参阅如何在 Amazon Keyspaces 中配置连接。重试策略 - 实施 Amazon Keyspaces 重试策略
AmazonKeyspacesExponentialRetryPolicy,而非 Cassandra 驱动程序附带的DefaultRetryPolicy。这样您就可以根据自己的需求配置AmazonKeyspacesExponentialRetryPolicy的重试次数。默认情况下,AmazonKeyspacesExponentialRetryPolicy的重试次数设置为 3。有关更多信息,请参阅 如何在 Amazon Keyspaces 中配置连接的重试策略。准备的语句 - 将
prepare-on-all-nodes设置为 false 以优化网络使用情况。
datastax-java-driver { basic { contact-points = [ "cassandra.us-east-2.amazonaws.com:9142"] request { timeout = 2 seconds consistency = LOCAL_QUORUM page-size = 1024 default-idempotence = true } load-balancing-policy { local-datacenter = "us-east-2" class = DefaultLoadBalancingPolicy slow-replica-avoidance = false } } advanced { auth-provider { class = software.aws.mcs.auth.SigV4AuthProvider aws-region =us-east-2} ssl-engine-factory { class = DefaultSslEngineFactory truststore-path = "./src/main/resources/cassandra_truststore.jks" truststore-password = "my_password" hostname-validation = false } connection { connect-timeout = 5 seconds max-requests-per-connection = 512 pool { local.size = 3 } } retry-policy { class = com.aws.ssa.keyspaces.retry.AmazonKeyspacesExponentialRetryPolicy max-attempts = 3 min-wait = 10 mills max-wait = 100 mills } prepared-statements { prepare-on-all-nodes = false } } }
注意
除了在配置文件中添加 trustStore 的路径外,您还可以直接在应用程序代码中添加 trustStore 的路径,或者在 JVM 参数中添加 trustStore 的路径。
步骤 3:运行应用程序
本代码示例展示了一个简单的命令行应用程序,它使用我们之前创建的配置文件创建了一个连接到 Amazon Keyspaces 的连接池。它通过运行一个简单的查询来确认连接已建立。
package<your package>; // add the following imports to your project import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.config.DriverConfigLoader; import com.datastax.oss.driver.api.core.cql.ResultSet; import com.datastax.oss.driver.api.core.cql.Row; public class App { public static void main( String[] args ) { //Use DriverConfigLoader to load your configuration file DriverConfigLoader loader = DriverConfigLoader.fromClasspath("application.conf"); try (CqlSession session = CqlSession.builder() .withConfigLoader(loader) .build()) { ResultSet rs = session.execute("select * from system_schema.keyspaces"); Row row = rs.one(); System.out.println(row.getString("keyspace_name")); } } }
注意
使用 try 代码块建立连接,以确保连接始终处于关闭状态。如果不使用 try 代码块,请记得关闭连接,以免泄漏资源。
使用适用于 Apache Cassandra 的 3.x DataStax Java 驱动程序和 SigV4 身份验证插件连接亚马逊密钥空间
以下部分介绍如何使用适用于 Apache Cassandra 的 3.x 开源 DataStax Java 驱动程序的 Sigv4 身份验证插件访问亚马逊密钥空间。该插件可从GitHub 存储库
使用 SigV4 身份验证插件,您可以在连接 Amazon Keyspaces 时为用户和角色使用 IAM 凭据。该插件不需要用户名和密码,而是使用访问密钥签署 API 请求。有关更多信息,请参阅 为 Amazon Keyspaces 创建和配置 AWS 证书。
步骤 1:先决条件
要运行此代码示例,您首先需要完成以下任务。
按照为 Amazon Keyspaces 创建和配置 AWS 证书中的步骤为 IAM 用户或角色创建凭证。本教程假设访问密钥将存储为环境变量。有关更多信息,请参阅 存储用于通过编程方式进行访问的访问密钥。
按照中的步骤下载数字证书,将其转换为 TrustStore 文件,然后将 JVM 参数中的密钥库附加到您的应用程序。开始前的准备工作
将 Apache Cassandra 的 DataStax Java 驱动程序添加到你的 Java 项目中。确保您使用的驱动程序版本支持 Apache Cassandra 3.11.2。有关更多信息,请参阅 A pache Cassandra 的 DataStax Java 驱动程序
文档。 将身份验证插件添加到您的应用程序。该身份验证插件支持 Apache Cassandr DataStax a 的 Java 驱动程序版本 3.x。如果您使用的是 Apache Maven 或可以使用 Maven 依赖关系的构建系统,请将以下依赖关系添加到您的
pom.xml文件中。将插件版本替换为GitHub 存储库中显示的最新版本。 <dependency> <groupId>software.aws.mcs</groupId> <artifactId>aws-sigv4-auth-cassandra-java-driver-plugin_3</artifactId> <version>3.0.3</version> </dependency>
步骤 2:运行应用程序
本代码示例展示了一个简单的命令行应用程序,它创建了一个连接到 Amazon Keyspaces 的连接池。它通过运行一个简单的查询来确认连接已建立。
package<your package>; // add the following imports to your project import software.aws.mcs.auth.SigV4AuthProvider; import com.datastax.driver.core.Cluster; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; import com.datastax.driver.core.Session; public class App { public static void main( String[] args ) { String endPoint = "cassandra.us-east-2.amazonaws.com"; int portNumber = 9142; Session session = Cluster.builder() .addContactPoint(endPoint) .withPort(portNumber) .withAuthProvider(new SigV4AuthProvider("us-east-2")) .withSSL() .build() .connect(); ResultSet rs = session.execute("select * from system_schema.keyspaces"); Row row = rs.one(); System.out.println(row.getString("keyspace_name")); } }
使用说明:
有关可用端点的列表,请参阅Amazon Keyspaces 的服务端点。
请参阅以下存储库,获取实用的 Java 驱动程序策略、示例,以及结合使用 Java 驱动程序和 Amazon Keyspaces 的最佳实践,:https://github.com/aws-samples/amazon-keyspaces-java-driver-helpers