本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Cassandra Go 客户端驱动程序以编程方式访问 Amazon Keyspaces
本节介绍如何使用 Go 客户端驱动程序连接到 Amazon Keyspaces。要向用户和应用程序提供对 Amazon Keyspaces 资源进行编程访问的凭证,您可以执行以下任一操作:
-
创建与特定 AWS AWS Identity and Access Management (IAM) 用户关联的服务特定凭证。
-
对临时凭证使用身份验证插件。此插件使 IAM 用户、角色和联合身份能够使用 AWS 签名版本 4 流程 (SigV4 将身份验证信息添加到 Amazon Keyspaces (for Apache Cassandra) API 请求中。
主题
开始前的准备工作
您需要先完成以下任务,然后才能启动 。
Amazon Keyspaces 需要使用传输层安全性 (TLS) 来帮助保护与客户端的连接。要使用 TLS 连接到 Amazon Keyspaces,您需要下载 Amazon 数字证书并将 Python 驱动程序配置为使用 TLS。
使用以下命令下载 Starfield 数字证书并将其保存到
目录中。
path_to_file
/
您还可以使用 Amazon 数字证书连接到 Amazon Keyspaces,并可以在客户端成功连接到 Amazon Keyspaces 时继续执行此操作。Starfield 证书为使用旧证书颁发机构的客户端提供额外的向后兼容性。
curl https://certs.secureserver.net/repository/sf-class2-root.crt -O
使用适用于 Apache Cassandra 的 Gocql 驱动程序和特定于服务的凭证连接到 Amazon Keyspaces
-
为您的应用程序创建一个目录。
mkdir ./gocqlexample
-
导航到新目录。
cd gocqlexample
-
为您的应用程序创建一个 文件。
touch cqlapp.go
-
下载 Go 驱动程序
go get github.com/gocql/gocql
-
将以下示例代码添加到 cqlapp.go 文件。
package main import ( "fmt" "github.com/gocql/gocql" "log" ) func main() { // add the Amazon Keyspaces service endpoint cluster := gocql.NewCluster("
cassandra.us-east-2.amazonaws.com
:9142") // add your service specific credentials cluster.Authenticator = gocql.PasswordAuthenticator{ Username: "ServiceUserName
", Password: "ServicePassword
"} // provide the path to the sf-class2-root.crt cluster.SslOpts = &gocql.SslOptions{ CaPath: "path_to_file
/sf-class2-root.crt", } // Override default Consistency to LocalQuorum cluster.Consistency = gocql.LocalQuorum // Disable initial host lookup cluster.DisableInitialHostLookup = true session, err := cluster.CreateSession() if err != nil { fmt.Println("err>", err) } defer session.Close() // run a sample query from the system keyspace var text string iter := session.Query("SELECT keyspace_name FROM system_schema.tables;").Iter() for iter.Scan(&text) { fmt.Println("keyspace_name:", text) } if err := iter.Close(); err != nil { log.Fatal(err) } session.Close() }使用说明:
-
将
"
替换为第一步中保存的证书的路径。path_to_file
/sf-class2-root.crt" -
确保
ServiceUserName
和ServicePassword
与您在生成服务特定凭证时通过执行步骤获取的用户名和密码匹配生成服务特定凭证。 -
有关可用终端节点的列表,请参阅Amazon Keyspaces 的服务终端节点。
-
-
构建程序
go build cqlapp.go
-
执行程序
./cqlapp
使用适用于 Apache Cassandra 的 Go 驱动程序和 SigV4 身份验证插件连接到 Amazon Keyspaces
以下代码示例演示如何使用开源 Go 驱动程序的 SigV4 身份验证插件访问 Amazon Keyspaces(对于 Apache Cassandra)。GitHub 存储库
将 Go SigV4 身份验证插件添加到您的应用程序。该插件支持适用于 Cassandra 的开源 Go 驱动程序 4.x 版,并依赖于适用于 Go 的 AWS 开发工具包。
$ go mod init $ go get github.com/aws/aws-sigv4-auth-cassandra-gocql-driver-plugin
在此代码示例中,Amazon Keyspaces 终端节点由 Cluster
类表示。它使用 AwsAuthenticator
作为集群的 身份验证器属性来获取凭证。
package main import ( "fmt" "github.com/aws/aws-sigv4-auth-cassandra-gocql-driver-plugin/sigv4" "github.com/gocql/gocql" "log" ) func main() { // configuring the cluster options cluster := gocql.NewCluster("
cassandra.us-west-2.amazonaws.com
:9142") var auth sigv4.AwsAuthenticator = sigv4.NewAwsAuthenticator() auth.Region = "us-west-2
" auth.AccessKeyId = "AKIAIOSFODNN7EXAMPLE
" auth.SecretAccessKey = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
" cluster.Authenticator = auth cluster.SslOpts = &gocql.SslOptions{ CaPath: "path_to_file
/sf-class2-root.crt", } cluster.Consistency = gocql.LocalQuorum cluster.DisableInitialHostLookup = true session, err := cluster.CreateSession() if err != nil { fmt.Println("err>", err) return } defer session.Close() // doing the query var text string iter := session.Query("SELECT keyspace_name FROM system_schema.tables;").Iter() for iter.Scan(&text) { fmt.Println("keyspace_name:", text) } if err := iter.Close(); err != nil { log.Fatal(err) } }
使用说明:
-
将
"
替换为第一步中保存的证书的路径。path_to_file
/sf-class2-root.crt" -
确保
AccessKeyId
和SecretAccessKey
与您使用 获取的访问密钥和秘密访问密钥匹配AwsAuthenticator
。有关更多信息,请参阅适用于 Go https://docs.amazonaws.cn/sdk-for-go/v1/developer-guide/configuring-sdk.html 的 AWS 开发工具包中的配置适用于 Go 的 AWS 开发工具包。 -
有关可用终端节点的列表,请参阅Amazon Keyspaces 的服务终端节点。