Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Use GetKeyspace
with an Amazon SDK or CLI
The following code examples show how to use GetKeyspace
.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in
context in the following code example:
- .NET
-
- Amazon SDK for .NET
-
/// <summary>
/// Get data about a keyspace.
/// </summary>
/// <param name="keyspaceName">The name of the keyspace.</param>
/// <returns>The Amazon Resource Name (ARN) of the keyspace.</returns>
public async Task<string> GetKeyspace(string keyspaceName)
{
var response = await _amazonKeyspaces.GetKeyspaceAsync(
new GetKeyspaceRequest { KeyspaceName = keyspaceName });
return response.ResourceArn;
}
- Java
-
- SDK for Java 2.x
-
public static void checkKeyspaceExistence(KeyspacesClient keyClient, String keyspaceName) {
try {
GetKeyspaceRequest keyspaceRequest = GetKeyspaceRequest.builder()
.keyspaceName(keyspaceName)
.build();
GetKeyspaceResponse response = keyClient.getKeyspace(keyspaceRequest);
String name = response.keyspaceName();
System.out.println("The " + name + " KeySpace is ready");
} catch (KeyspacesException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
- Kotlin
-
- SDK for Kotlin
-
suspend fun checkKeyspaceExistence(keyspaceNameVal: String?) {
val keyspaceRequest =
GetKeyspaceRequest {
keyspaceName = keyspaceNameVal
}
KeyspacesClient { region = "us-east-1" }.use { keyClient ->
val response: GetKeyspaceResponse = keyClient.getKeyspace(keyspaceRequest)
val name = response.keyspaceName
println("The $name KeySpace is ready")
}
}
- Python
-
- SDK for Python (Boto3)
-
class KeyspaceWrapper:
"""Encapsulates Amazon Keyspaces (for Apache Cassandra) keyspace and table actions."""
def __init__(self, keyspaces_client):
"""
:param keyspaces_client: A Boto3 Amazon Keyspaces client.
"""
self.keyspaces_client = keyspaces_client
self.ks_name = None
self.ks_arn = None
self.table_name = None
@classmethod
def from_client(cls):
keyspaces_client = boto3.client("keyspaces")
return cls(keyspaces_client)
def exists_keyspace(self, name):
"""
Checks whether a keyspace exists.
:param name: The name of the keyspace to look up.
:return: True when the keyspace exists. Otherwise, False.
"""
try:
response = self.keyspaces_client.get_keyspace(keyspaceName=name)
self.ks_name = response["keyspaceName"]
self.ks_arn = response["resourceArn"]
exists = True
except ClientError as err:
if err.response["Error"]["Code"] == "ResourceNotFoundException":
logger.info("Keyspace %s does not exist.", name)
exists = False
else:
logger.error(
"Couldn't verify %s exists. Here's why: %s: %s",
name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
return exists
For a complete list of Amazon SDK developer guides and code examples, see
Using Amazon Keyspaces with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.