Create a keyspace
Creates a new keyspace in a database.
Keyspaces are used to store collections and tables in Hyper-Converged Database (HCD).
Result
-
Python
-
TypeScript
-
Java
-
curl
Creates the specified keyspace.
Does not return anything.
Creates the specified keyspace.
Returns a promise that resolves once the operation completes.
Creates the specified keyspace.
Does not return anything.
Creates the specified keyspace.
If the command succeeds, the response indicates the success.
Example successful response:
{
"status": {
"ok": 1
}
}
Parameters
-
Python
-
TypeScript
-
Java
-
curl
Use the create_keyspace
method, which belongs to the astrapy.DataAPIDatabaseAdmin
class.
Method signature
create_keyspace(
name: str,
*,
replication_options: dict[str, Any],
update_db_keyspace: bool,
keyspace_admin_timeout_ms: int,
request_timeout_ms: int,
timeout_ms: int,
**kwargs: Any,
) -> None
Name | Type | Summary |
---|---|---|
|
|
The name of the keyspace to create. If a keyspace with the name already exists, a new keyspace isn’t created, and the command succeeds. |
|
|
Optional. Specifies the options for keyspace replication across database nodes. The structure must be similar to: |
|
|
Optional.
Whether to set the working keyspace of the |
|
|
Optional.
A timeout, in milliseconds, to impose on the underlying API request.
If not provided, the This parameter is aliased as |
Use the createKeyspace
method, which belongs to the DataAPIDbAdmin
class.
Method signature
async createKeyspace(
keyspace: string,
options?: {
replication?: KeyspaceReplicationOptions,
updateDbKeyspace?: boolean,
timeout?: number | TimeoutDescriptor,
}
): void
Name | Type | Summary |
---|---|---|
|
|
The name of the keyspace to create. If a keyspace with the name already exists, a new keyspace isn’t created, and the command succeeds. |
|
|
Optional.
The options for this operation. See Properties of |
Name | Type | Summary |
---|---|---|
|
|
Optional. Specifies the options for keyspace replication across database nodes. Default: |
|
|
Optional.
Whether to set the working keyspace of the |
|
|
Optional. The timeout(s) to apply to this method.
You can specify Details about the
|
Use the createKeyspace
method, which belongs to the com.datastax.astra.client.admin.DatabaseAdmin
class.
Method signature
void createKeyspace(String keyspaceName)
void createKeyspace(KeyspaceDefinition keyspaceDefinition)
void createKeyspace(
KeyspaceDefinition keyspaceDefinition,
CreateKeyspaceOptions options
)
Name | Type | Summary |
---|---|---|
|
|
The name of the keyspace to create. |
|
|
The keyspace name and optionally replication factors. See Methods of |
|
|
Optional.
The options for this operation. See Methods of |
Method | Parameters | Summary |
---|---|---|
|
|
The name of the keyspace to create. |
|
|
Specifies the options for keyspace replication across database nodes using a simple strategy. |
|
|
Specifies the options for keyspace replication across database nodes using a network topology strategy. |
Method | Parameters | Summary |
---|---|---|
|
|
Optional.
Whether to set the working keyspace of the Default: false |
|
|
Optional. Whether the command should silently succeed even if a keyspace with the given name already exists and no new keyspace was created. Default: true |
Use the createKeyspace
command.
Command signature
curl -sS -L -X POST "API_ENDPOINT/v1" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"createKeyspace": {
"name": "KEYSPACE_NAME",
"options": {
"replication": REPLICATION_STRATEGY,
}
}
}'
Name | Type | Summary |
---|---|---|
|
|
The name of the keyspace to create. If a keyspace with the name already exists, a new keyspace isn’t created, and the command succeeds. |
|
|
Optional. Specifies the options for keyspace replication across database nodes. The structure must be similar to: |
Examples
The following examples demonstrate how to create a keyspace.
Create a keyspace
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get a database
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
# Get an admin object
admin = database.get_database_admin()
# Create a keyspace
admin.create_keyspace("KEYSPACE_NAME")
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get a database
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
// Get an admin object
const admin = database.admin({ environment: "hcd" });
// Create a keyspace
(async function () {
await admin.createKeyspace("KEYSPACE_NAME");
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.admin.DatabaseAdmin;
import com.datastax.astra.client.databases.Database;
public class Example {
public static void main(String[] args) {
// Get a database
DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
// Get an admin object
DatabaseAdmin admin = database.getDatabaseAdmin();
// Create a keyspace
admin.createKeyspace("KEYSPACE_NAME");
}
}
curl -sS -L -X POST "API_ENDPOINT/v1" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"createKeyspace": {
"name": "KEYSPACE_NAME"
}
}'
Create a keyspace and set it as the working keyspace for the database
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get a database
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
# Get an admin object
admin = database.get_database_admin()
# Create a keyspace
admin.create_keyspace("KEYSPACE_NAME", update_db_keyspace=True)
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get a database
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
// Get an admin object
const admin = database.admin({ environment: "hcd" });
// Create a keyspace
(async function () {
await admin.createKeyspace("KEYSPACE_NAME", { updateDbKeyspace: true });
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.admin.DatabaseAdmin;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.databases.commands.options.CreateKeyspaceOptions;
import com.datastax.astra.client.databases.definition.keyspaces.KeyspaceDefinition;
public class Example {
public static void main(String[] args) {
// Get a database
DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
// Get an admin object
DatabaseAdmin admin = database.getDatabaseAdmin();
// Create a keyspace
KeyspaceDefinition definition = new KeyspaceDefinition().name("KEYSPACE_NAME");
CreateKeyspaceOptions keyspaceOptions = new CreateKeyspaceOptions().updateDBKeyspace(true);
admin.createKeyspace(definition, keyspaceOptions);
}
}
This option only applies to the clients.
Create a keyspace and specify replication options
-
Python
-
TypeScript
-
Java
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get a database
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
# Get an admin object
admin = database.get_database_admin()
# Create a keyspace
admin.create_keyspace(
"KEYSPACE_NAME",
replication_options={"class": "SimpleStrategy", "replication_factor": 3},
)
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get a database
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
// Get an admin object
const admin = database.admin({ environment: "hcd" });
// Create a keyspace
(async function () {
await admin.createKeyspace("KEYSPACE_NAME", {
replication: {
class: "SimpleStrategy",
replicationFactor: 3,
},
});
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.admin.DatabaseAdmin;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.databases.definition.keyspaces.KeyspaceDefinition;
public class Example {
public static void main(String[] args) {
// Get a database
DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
// Get an admin object
DatabaseAdmin admin = database.getDatabaseAdmin();
// Create a keyspace
KeyspaceDefinition definition =
new KeyspaceDefinition().name("KEYSPACE_NAME").simpleStrategy(3);
admin.createKeyspace(definition);
}
}
curl -sS -L -X POST "API_ENDPOINT/v1" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"createKeyspace": {
"name": "KEYSPACE_NAME",
"options": {
"replication": {
"class": "SimpleStrategy", "replication_factor": 3
}
}
}
}'
Client reference
-
Python
-
TypeScript
-
Java
-
curl
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
Client reference documentation is not applicable for HTTP.