Creating a table in Cassandra
Last Updated :
28 Apr, 2020
In this article, we are going to discuss how we can create a table in Cassandra and also discuss column definitions, keys role, Table options (compact storage and clustering order), etc.
In Cassandra, the CQL table has a name and it stores rows. when you create a table, you define the columns for the rows, a mandatory primary key to identify each row, column data type, and any other additional you may choose.
To create a table used “creating a table” statement given below as following.
The following is a typical table creation statement.
Syntax: Creating a Table.
CREATE TABLE [ IF NOT EXISTS ] table_name
'('
column_definition
( ', ' column_definition )*
[ ', ' PRIMARY KEY '(' primary_key ')' ]
')' [ WITH table_options ]
Now, here you can use any existing keyspace such as named App_data.
use App_data;
Now, you can create table User_data in which Name, id, address are the fields in the table.
CREATE TABLE User_data (
Name text,
id uuid,
address text,
PRIMARY KEY (id)
);
Now, you can verify the table whether it is created or not, and if it is created then verify the table definition.
By using existing keyspace such as App_data.
cassandra@cqlsh> use App_data;
cassandra@cqlsh:app_data>
cassandra@cqlsh:app_data> CREATE TABLE User_data (
... id uuid,
... Name text,
... address text,
... PRIMARY KEY (id)
... );
In Cassandra, A primary key consists of the first column or columns is the mandatory partition key, followed by one or more clustering columns.
COLUMN_DEFINITION
In Cassandra, a column_definition clause consists of the name of the column and its type, as well as two modifiers.
Static: In Cassandra, a static column has the same value for all rows that share the same partition key (explained in a little bit). Of course, only non-primary keys can be static.
Primary key: A primary key uniquely identifies a row, and It is a good practice all tables must define a primary key.
Now, here you can verify the created table User_data.
cassandra@cqlsh:app_data> describe User_data;
CREATE TABLE app_data.user_data (
id uuid PRIMARY KEY,
address text,
name text
) WITH bloom_filter_fp_chance = 0.01
AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
AND comment = ''
AND compaction =
{
'class': 'org.apache.cassandra.db.compaction
.SizeTieredCompactionStrategy',
'max_threshold': '32', 'min_threshold': '4'
}
AND compression = {'chunk_length_in_kb': '64',
'class': 'org.apache.cassandra.io
.compress.LZ4Compressor'}
AND crc_check_chance = 1.0
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99PERCENTILE';
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...