Sunday 12 January 2020

Cassandra: Create a keyspace


‘CREATE KEYSPACE’ command is used to create a keyspace.

Syntax
CREATE  KEYSPACE [IF NOT EXISTS] keyspace_name
   WITH REPLICATION = {replication_map}
   [AND DURABLE_WRITES =  true|false] ;

Example
CREATE KEYSPACE cassandraTutorial WITH REPLICATION = {
  'class' : 'NetworkTopologyStrategy',
  'DC1' : 3
} AND DURABLE_WRITES = false;

Step 1: Start Cassandra instance by running the command ‘cassandra -f’.

Step 2: Open another terminal and execute the command cqlsh.
$ cqlsh
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh>


Step 3: Execute below command to create a keyspace ‘cassandraTutorial’.
CREATE KEYSPACE cassandraTutorial WITH REPLICATION = {
  'class' : 'NetworkTopologyStrategy',
  'DC1' : 3
} AND DURABLE_WRITES = false;


Step 4: You can execute the command ‘DESCRIBE cassandraTutorial’, to know about the keyspace ‘cassandraTutorial’.
cqlsh> DESCRIBE cassandraTutorial;

CREATE KEYSPACE cassandratutorial WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1': '3'}  AND durable_writes = false;

keyspace_name
a. A keyspace name must contain alpha-numeric characters and underscore.
b. Only letters and numbers are used as first character of keyspace name.
c. A keyspace name must have maximum of 48 characters.

d. Unquoted keyspace name is converted to lowercase. For example, I created a key space with name ‘cassandraTutorial’, but internally it is created as ‘cassandratutorial’ (T in the Tutorial is lowered). To persist the case of letters use quotes while creating the keyspace.

cqlsh> DROP KEYSPACE cassandraTutorial;
cqlsh> 
cqlsh> CREATE KEYSPACE "cassandraTutorial" WITH REPLICATION = {
   ...   'class' : 'NetworkTopologyStrategy',
   ...   'DC1' : 3
   ... } AND DURABLE_WRITES = false;
cqlsh> 
cqlsh> DESCRIBE "cassandraTutorial";

CREATE KEYSPACE "cassandraTutorial" WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1': '3'}  AND durable_writes = false;

replication_map
‘replication_map’ is used to specify replication strategy, replication factor etc.,

DURABLE_WRITES

It is set to true by default. If you set this value to false, then Cassandra bypasses the commit log when writing to the keyspace.


Previous                                                    Next                                                    Home

No comments:

Post a Comment