Keyspaces

Keyspaces are used to store collections in Hyper-Converged Database (HCD). To manage keyspaces, you must first get a database admin object.

HCD APIs use the term keyspace to refer to both namespaces and keyspaces.

Keyspaces are the main database objects used to store collections in HCD. It’s a good practice to create a keyspace for each application.

View this topic in more detail on the API Reference.

Create a new keyspace in the database:

python
database = DataAPIClient(
    token=token_provider, environment=Environment.HCD,
).get_database(DB_API_ENDPOINT)
database.get_database_admin().create_keyspace(DB_KEYSPACE)

Create a new keyspace in the database, while setting it as the new working keyspace for the database:

python
database = DataAPIClient(
    token=token_provider, environment=Environment.HCD,
).get_database(DB_API_ENDPOINT)
database.get_database_admin().create_keyspace(
    DB_KEYSPACE,
    update_db_keyspace=True,
)

Create a new keyspace in the database, explicitly setting its replication options:

python
database = DataAPIClient(
    token=token_provider, environment=Environment.HCD,
).get_database(DB_API_ENDPOINT)
database.get_database_admin().create_keyspace(
    DB_KEYSPACE,
    replication_options={"class": "SimpleStrategy", "replication_factor": 3},
)

Parameters for get_database_admin:

Name Type Summary

token

Optional[Union[TokenProvider, str]]

The authentication token. It can be a string or an instance of an astrapy.authentication.TokenProvider. If provided, overrides the token configured for the Database object.

Parameters for create_keyspace:

Name Type Summary

name

str

The keyspace name. If supplying a keyspace that exists already, the method call proceeds as usual, no errors are raised, and the whole invocation is a no-op.

replication_options

Optional[Dict[str, Any]]

This dictionary can specify the options for namespace replication across database nodes. If provided, it must have a structure similar to: {"class": "SimpleStrategy", "replication_factor": 1}. See also hcd@cql:get-started:cql-quickstart.adoc#create-a-keyspace.

update_db_keyspace

Optional[bool]

if True, the Database or AsyncDatabase class that created this DatabaseAdmin is updated to work on the newly-created keyspace when this method returns.

Returns:

Dict - A dictionary of the form {"ok": 1} if the operation succeeds. Otherwise, an exception is raised.

Example response
{"ok": 1}

Example:

python
import os

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Database settings
DB_USERNAME = "cassandra"
DB_PASSWORD = "cassandra"
DB_API_ENDPOINT = "http://localhost:8181"
DB_KEYSPACE = "cycling"

# Prepare a token provider
token_provider = UsernamePasswordTokenProvider(DB_USERNAME, DB_PASSWORD)

# Initialize the client and get a "Database" object
client = DataAPIClient(token=token_provider, environment=Environment.HCD)
database = client.get_database(DB_API_ENDPOINT)

# Create the keyspace; also set it as the working keyspace for the database
database.get_database_admin().create_keyspace(
    DB_KEYSPACE,
    update_db_keyspace=True,
)

print(database.keyspace == DB_KEYSPACE)  # True

The get_database_admin method of Database (or AsyncDatabase) always returns a subclass of DatabaseAdmin. For HCD, you will get a DataAPIDatabaseAdmin.

If you were working in a cloud environment such as Astra DB—for example environment=Environment.PROD, this method would return an AstraDBDatabaseAdmin, reflecting the differences between the underlying infrastructure. In that case, there would be no support for the replication settings.

List keyspaces

Get a list of the keyspace found in a database.

View this topic in more detail on the API Reference.

python
keyspace = database.get_database_admin().list_keyspaces()

Parameters:

Name Type Summary

max_time_ms

Optional[int]

A timeout, in milliseconds, for the whole requested operation to complete.

Returns:

List[str] - A list of the keyspaces with their names appearing in no particular order.

Example response
['cycling', 'food']

Example:

python
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Database settings
DB_USERNAME = "cassandra"
DB_PASSWORD = "cassandra"
DB_API_ENDPOINT = "http://localhost:8181"

# Prepare a token provider
token_provider = UsernamePasswordTokenProvider(DB_USERNAME, DB_PASSWORD)

# Initialize the client and get a "Database" object
database = DataAPIClient(
    token=token_provider, environment=Environment.HCD,
).get_database(DB_API_ENDPOINT)

# Get a list of the keyspaces in the database
keyspaces = database.get_database_admin().list_keyspaces()

Drop a keyspace

Drop (delete) a keyspace in a database, erasing all data stored in it as well.

View this topic in more detail on the API Reference.

python
database.get_database_admin().drop_keyspace(DB_KEYSPACE)

Parameters:

Name Type Summary

name

str

The keyspace to delete. If it does not exist in this database, an error is raised.

max_time_ms

Optional[int]

A timeout, in milliseconds, for the whole requested operation to complete. Note that a timeout is no guarantee that the deletion request has not reached the API server.

Returns:

Dict - A dictionary of the form {"ok": 1} if the operation succeeds. Otherwise, an exception is raised.

Example response
{"ok": 1}

Example:

python
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Database settings
DB_USERNAME = "cassandra"
DB_PASSWORD = "cassandra"
DB_API_ENDPOINT = "http://localhost:8181"
OLD_DB_KEYSPACE = "cycling_legacy"

# Prepare a token provider
token_provider = UsernamePasswordTokenProvider(DB_USERNAME, DB_PASSWORD)

# Initialize the client and get a "Database" object
database = DataAPIClient(
    token=token_provider, environment=Environment.HCD,
).get_database(DB_API_ENDPOINT)

# Drop a keyspace from the database
drop_response = database.get_database_admin().drop_keyspace(OLD_DB_KEYSPACE)

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax | Privacy policy | Terms of use | Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com