Manage keyspaces
Astra DB Classic databases organize data into tables, and tables are grouped into keyspaces. Keyspaces are like schemas in relational databases. They help keep related tables of information in a single place within your database.
Keyspaces contain tables, materialized views and user-defined types, functions, and aggregates. Typically, a cluster has one keyspace per application.
Replication strategy and factor
A keyspace is the top-level database object that controls the replication for the object it contains at each datacenter in the cluster.
Because replication is controlled on a per-keyspace basis, you can store data with different replication requirements in the same region in different keyspaces. Keyspaces are not a significant map layer within the data model.
Astra DB uses the NetworkTopologyStrategy
, which is controlled by Astra DB and cannot be modified.
In Astra DB, keyspaces hold the datacenter names associated with your database regions, and they define the replication factor for each datacenter, which is 3
for writes in LOCAL_QUORUM
.
A replication factor of three reduces the chance of data loss.
Create a keyspace
A keyspace is a container for tables, similar to a schema in a relational database. Keyspaces help you organize related tables. For example, you can have different data models for each keyspace or store unique data in unique keyspaces.
You can use multiple keyspaces in a single region to build applications on a per-keyspace data model.
You can create keyspaces in the Astra Portal or with the DevOps API.
-
Astra Portal
-
API/CLI
-
In the Astra Portal, go to Databases, and then click your Astra DB Classic database’s name.
-
Click Add Keyspace.
-
Enter a keyspace name that meets the following requirements:
-
Only alphanumeric characters and underscores
-
No more than 48 characters
-
Isn’t
dse
orsystem
-
-
Click Save.
You can use CQL, the DevOps API, and the Astra CLI to create keyspaces programmatically.
For example, you can use the following curl command to create a keyspace:
curl -sS --location -X POST "https://api.astra.datastax.com/v2/databases/DB_ID/keyspaces/KEYSPACE_NAME" \
--header "Authorization: Bearer APPLICATION_TOKEN" \
--header "Content-Type: application/json"
The database enters Maintenance status while it creates the keyspace. When the database returns to Active status, you can use the new keyspace. For example, you can create a table and load data into the keyspace.
Get keyspace details
Use the CQL DESCRIBE KEYSPACE
command to get the full details of a keyspace, including all database objects the keyspace contains.
You can use the output from a DESCRIBE
command to recreate the keyspace and all its components in another database.
The output includes all settings for tables and other objects including the default settings. When recreating a keyspace in another environment, verify that the settings are compatible, such as the keyspace replication factor datacenter names.
Delete a keyspace
Deleting a keyspace permanently deletes all tables and data in the keyspace. |
-
Astra Portal
-
API/CLI
-
In the Astra Portal, go to Databases, and then click your database’s name.
-
In the Keyspaces section, locate the keyspace you want to delete, click
More, and then click Delete. -
In the Delete Keyspace dialog, enter the keyspace name, and then click Delete Keyspace.
You can use CQL, the DevOps API}, and the Astra CLI to delete keyspaces programmatically.
For example, you can use the following curl command to delete a keyspace:
curl -sS -location -X DELETE "https://api.astra.datastax.com/v2/databases/DB_ID/keyspaces/KEYSPACE_NAME" \
--header "Authorization: Bearer APPLICATION_TOKEN" \
--header "Content-Type: application/json"
The database enters Maintenance status while it deletes the keyspace, its tables, and any data within those tables.
Create a table
You can use the CQL shell to create a table through the Astra Portal, a CQL driver, or the standalone CQLSH client.
For information about the CQL shell and instructions for using the CQL drivers or CQLSH client to create tables, see Developing with CQL.
To use the CQL shell in the Astra Portal to create an empty table, do the following:
-
In the Astra Portal, go to Databases, and then click your database.
-
In the Keyspaces section, note the name of the keyspace where you want to create the table.
-
Click CQL Console, and then wait for the
token@cqlsh>
prompt to appear. -
Select the keyspace that you want to create the table in:
use KEYSPACE_NAME;
-
Create the table:
CREATE TABLE users ( firstname text, lastname text, email text, "favorite color" text, PRIMARY KEY (firstname, lastname) ) WITH CLUSTERING ORDER BY (lastname ASC);
After you create a table, load data into the table.
Delete a table
You can use the CQL shell to delete a table through the Astra Portal, a CQL driver, or the standalone CQLSH client.
For information about the CQL shell and instructions for using the CQL drivers or CQLSH client to delete tables, see Developing with CQL.
Deleting a table permanently deletes all data in the table. |
To use the CQL shell in the Astra Portal to delete a table, do the following:
-
In the Astra Portal, go to Databases, and then click your database.
-
In the Keyspaces section, note the name of the keyspace that contains the table you want to delete.
-
Click CQL Console, and then wait for the
token@cqlsh>
prompt to appear. -
Select the keyspace that contains the table you want to delete:
use KEYSPACE_NAME;
-
Get a list of all tables in the keyspace:
desc tables;
-
Delete the table and all of its data:
drop table TABLE_NAME;
The table and its data are deleted.