Keyspaces
Keyspaces are used to store collections in DataStax Enterprise (DSE). In order to create a keyspace, you must first get a database admin object.
Create a keyspace
Keyspaces are the main database objects used to store collections in DataStax Enterprise (DSE). In general, you should create a keyspace for each application.
Create a keyspace with the Python client
View this topic in more detail on the API Reference.
database = client.get_database_by_api_endpoint(token=tp, api_endpoint=DB_API_ENDPOINT)
database.get_database_admin().create_keyspace(DB_KEYSPACE)
Parameters:
| Name | Type | Summary |
|---|---|---|
name |
|
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. |
Returns:
Dict - A dictionary of the form {"ok": 1} if the operation succeeds. Otherwise,
an exception is raised.
Example:
database.get_database_admin().create_keyspace(DB_KEYSPACE)
Create a keyspace with the TypeScript client
View this topic in more detail on the API Reference.
const dbAdmin = db.admin({ environment: DB_ENVIRONMENT });
(async () => {
await dbAdmin.createKeyspace(DB_KEYSPACE);
console.log(await dbAdmin.listKeyspaces());
})();
Parameters:
| Name | Type | Summary |
|---|---|---|
name |
|
The name of the keyspace to create. |
options? |
Blocking options regarding the creation of the keyspace. |
Returns:
Promise<void> - A promise that resolves when the keyspace is created (or when the initial request
completes if not blocking).
Example:
(async () => {
await dbAdmin.createKeyspace(DB_KEYSPACE);
console.log(await dbAdmin.listKeyspaces());
})();
|
The |
Create a keyspace with the Java client
// Create a default keyspace
((DataAPIDatabaseAdmin) client
.getDatabase(dataApiUrl)
.getDatabaseAdmin()).createKeyspace(keyspaceName, KeyspaceOptions.simpleStrategy(1));
Database db = client.getDatabase(dataApiUrl, keyspaceName);
Parameters:
| Name | Type | Summary |
|---|---|---|
keyspaceName |
|
The unique name for the keyspace |
KeyspaceOptions |
|
The replication class and replication factor for the keyspace |
Does not return anything.
Example:
// Create a default keyspace
((DataAPIDatabaseAdmin) client
.getDatabase(dataApiUrl)
.getDatabaseAdmin()).createKeyspace(keyspaceName, KeyspaceOptions.simpleStrategy(1));
System.out.println("3/7 - Keyspace '" + keyspaceName + "'created ");
Database db = client.getDatabase(dataApiUrl, keyspaceName);
System.out.println("4/7 - Connected to Database");
List keyspaces
Get a list of the keyspaces found in a database.
Get keyspaces with the Python client
View this topic in more detail on the API Reference.
keyspaces = database.get_database_admin().list_keyspaces()
Returns List[str], which is a list of the keyspaces with their names appearing in no particular order, such as ['cycling', 'food'].
Example:
database.get_database_admin().list_keyspaces()
Get keyspaces with the TypeScript client
View this topic in more detail on the API Reference.
(async () => {
console.log(await dbAdmin.listKeyspaces());
})();
Returns Promise<string[]>, which is a list of the keyspaces, with the first keyspace being the default one.
Example:
(async () => {
console.log(await dbAdmin.listKeyspaces());
})();
Get keyspaces with the Java client
// List all keyspaces
client.getDatabase(dataApiUrl).getDatabaseAdmin().listKeyspaces().forEach(System.out::println);
Accepts no parameters.
Returns Set<String>, which is the list of available keyspaces in current database.
Example:
// List all keyspaces
client.getDatabase(dataApiUrl).getDatabaseAdmin().listKeyspaces().forEach(System.out::println);
Drop a keyspace
Drop (delete) a keyspace in a database, erasing all data stored in it as well.
Drop a keyspace with the Python client
View this topic in more detail on the API Reference.
database.get_database_admin().drop_keyspace(DB_KEYSPACE)
Parameters:
| Name | Type | Summary |
|---|---|---|
name |
|
The keyspace to delete. If it does not exist in this database, an error is raised. |
wait_until_active |
|
If True (default), the method returns only after the target database is in ACTIVE state again (a few seconds, usually). If False, it will return right after issuing the deletion request to the DevOps API, and it will be responsibility of the caller to check the database status/keyspace availability before working with it. |
max_time_ms |
|
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, which is a dictionary of the form {"ok": 1} if the operation succeeds.
Otherwise, an exception is raised.
Example:
database.get_database_admin().drop_keyspace(DB_KEYSPACE)
Drop a keyspace with the TypeScript client
View this topic in more detail on the API Reference.
await dbAdmin.dropKeyspace(DB_KEYSPACE);
console.log('* Keyspace dropped.');
await client.close();
Parameters:
| Name | Type | Summary |
|---|---|---|
name |
|
The name of the keyspace to drop. |
options? |
Blocking options regarding the deletion of the keyspace. |
Returns Promise<void>, which is a promise that resolves when the keyspace is deleted (or when the initial request
completes if not blocking).
Example:
// Drop the keyspace
await dbAdmin.dropKeyspace(DB_KEYSPACE);
console.log('* Keyspace dropped.');
await client.close();
|
The |
Drop a keyspace with the Java client
((DataAPIDatabaseAdmin) client
.getDatabase(dataApiUrl)
.getDatabaseAdmin()).dropKeyspace(keyspaceName);
Parameters:
| Name | Type | Summary |
|---|---|---|
keyspace |
|
The name of the keyspace to delete. |
Example:
// Drop the keyspace
((DataAPIDatabaseAdmin) client
.getDatabase(dataApiUrl)
.getDatabaseAdmin()).dropKeyspace(keyspaceName);
System.out.println("8/7 - Keyspace '" + keyspaceName + "' dropped ");