使用软件开发工具包的 Amazon Keyspaces 的代码示例 Amazon - Amazon Keyspaces(Apache Cassandra 兼容)
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

使用软件开发工具包的 Amazon Keyspaces 的代码示例 Amazon

以下代码示例展示了如何将 Amazon Keyspaces 与 Amazon 软件开发套件 (SDK) 一起使用。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景是展示如何通过在同一服务中调用多个函数来完成特定任务任务的代码示例。

有关 S Amazon DK 开发者指南和代码示例的完整列表,请参阅将 Amazon Keyspaces 与 SDK 配合使用 Amazon。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。

开始使用

以下代码示例展示了如何开始使用 Amazon Keyspaces。

.NET
Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

namespace KeyspacesActions; public class HelloKeyspaces { private static ILogger logger = null!; static async Task Main(string[] args) { // Set up dependency injection for Amazon Keyspaces (for Apache Cassandra). using var host = Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.AddFilter("System", LogLevel.Debug) .AddFilter<DebugLoggerProvider>("Microsoft", LogLevel.Information) .AddFilter<ConsoleLoggerProvider>("Microsoft", LogLevel.Trace)) .ConfigureServices((_, services) => services.AddAWSService<IAmazonKeyspaces>() .AddTransient<KeyspacesWrapper>() ) .Build(); logger = LoggerFactory.Create(builder => { builder.AddConsole(); }) .CreateLogger<HelloKeyspaces>(); var keyspacesClient = host.Services.GetRequiredService<IAmazonKeyspaces>(); var keyspacesWrapper = new KeyspacesWrapper(keyspacesClient); Console.WriteLine("Hello, Amazon Keyspaces! Let's list your keyspaces:"); await keyspacesWrapper.ListKeyspaces(); } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NET API 参考ListKeyspaces中的。

Java
适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.keyspaces.KeyspacesClient; import software.amazon.awssdk.services.keyspaces.model.KeyspaceSummary; import software.amazon.awssdk.services.keyspaces.model.KeyspacesException; import software.amazon.awssdk.services.keyspaces.model.ListKeyspacesRequest; import software.amazon.awssdk.services.keyspaces.model.ListKeyspacesResponse; import java.util.List; /** * Before running this Java (v2) code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class HelloKeyspaces { public static void main(String[] args) { Region region = Region.US_EAST_1; KeyspacesClient keyClient = KeyspacesClient.builder() .region(region) .build(); listKeyspaces(keyClient); } public static void listKeyspaces(KeyspacesClient keyClient) { try { ListKeyspacesRequest keyspacesRequest = ListKeyspacesRequest.builder() .maxResults(10) .build(); ListKeyspacesResponse response = keyClient.listKeyspaces(keyspacesRequest); List<KeyspaceSummary> keyspaces = response.keyspaces(); for (KeyspaceSummary keyspace : keyspaces) { System.out.println("The name of the keyspace is " + keyspace.keyspaceName()); } } catch (KeyspacesException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考ListKeyspaces中的。

Kotlin
适用于 Kotlin 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html */ suspend fun main() { listKeyspaces() } suspend fun listKeyspaces() { val keyspacesRequest = ListKeyspacesRequest { maxResults = 10 } KeyspacesClient { region = "us-east-1" }.use { keyClient -> val response = keyClient.listKeyspaces(keyspacesRequest) response.keyspaces?.forEach { keyspace -> println("The name of the keyspace is ${keyspace.keyspaceName}") } } }
  • 有关 API 的详细信息,请参阅适用ListKeyspaces于 K otlin 的Amazon SDK API 参考

Python
SDK for Python (Boto3)
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

import boto3 def hello_keyspaces(keyspaces_client): """ Use the AWS SDK for Python (Boto3) to create an Amazon Keyspaces (for Apache Cassandra) client and list the keyspaces in your account. This example uses the default settings specified in your shared credentials and config files. :param keyspaces_client: A Boto3 Amazon Keyspaces Client object. This object wraps the low-level Amazon Keyspaces service API. """ print("Hello, Amazon Keyspaces! Let's list some of your keyspaces:\n") for ks in keyspaces_client.list_keyspaces(maxResults=5).get("keyspaces", []): print(ks["keyspaceName"]) print(f"\t{ks['resourceArn']}") if __name__ == "__main__": hello_keyspaces(boto3.client("keyspaces"))
  • 有关 API 的详细信息,请参阅适用ListKeyspacesPython 的Amazon SDK (Boto3) API 参考