Create or delete keyspace and table schema
In order to use the REST API, you must create schema that defines at least one
keyspace and one table that will store the data.
A keyspace is a container for which a replication factor
defines the number of
data replicas the database will store.
Tables consist of columns that each have a name and a defined data type.
Multiple tables are contained in a keyspace, but a table cannot be contained in
multiple keyspaces.
If you are connecting to a Cassandra database with existing schema, you can skip
this step.
For keyspaces, an optional setting, replicas
, defines
the number of data replicas the database will store for the keyspace.
If no replica is defined, then for a keyspace in a single datacenter cluster,
the default is 1, and for a multiple-datacenter cluster, the default is 3 for each
datacenter.
Create a keyspace
Simple keyspace
Send a POST
request to /api/rest/v2/schemas/keyspaces
.
In this example we use users_keyspace
for the name
, and no replicas
setting, to default to 1.
curl -s --location --request POST 'https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces' \
--header "X-Cassandra-Token: $AUTH_TOKEN" \
--header 'Content-Type: application/json' \
--data '{
"name": "users_keyspace"
}'
{"name":"users_keyspace"
The authorization token and the content type are passed with --header
. The
token must be identified as X-Cassandra-Token
so that cluster recognizes the token
and its value.
The specified name for the keyspace is passed as JSON data using --data
.
For shorthand, cURL
can use -L
for --location
, -X
for --request
, -H
for --header
, and -d
for --data
.
Set replicas in simple keyspace
Send a POST
request to /api/rest/v2/schemas/keyspaces
.
In this example we use users_keyspace
for the name
,
and 1
for the number of data replicas
.
curl -s -L -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "users_keyspace",
"replicas": 1
}'
{"name":"users_keyspace"
Keyspace for multiple datacenters
For a multiple-datacenter cluster, a keyspace is defined datacenters
.
Send a POST
request to /api/rest/v2/schemas/keyspaces
.
In this example we use users_keyspace-dcs
for the name
, the datacenters are dc1
and dc2
,
where dc1
defaults to 3 replicas and dc2
is set to 5 replicas.
curl -s -L -X POST 'https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces' \
-H "X-Cassandra-Token: $AUTH_TOKEN" \
-H 'Content-Type: application/json' \
-d '{
"name": "users_keyspace-dcs",
"datacenters": [ {"name": "dc1"}, {"name": "dc2", "replicas": 5} ]
}'
{"name":"users_keyspace-dcs"}