In this article we will discuss how TOKEN function works in Cassandra using partitioners. There are 3 different type of partitioners that is supported by Cassandra Query Language.
Partitioners in CQL :
1. Murmur3partitioner 2. RandomPartitioner 3. ByteOrderedPartitioner
Let’s discuss one by one.
- Murmur3partitioner :
It is default partitioner in Cassandra 3.0. If we used TOKEN function then it distribute data over cluster based on MurmurHash hash values. It is also useful to provide good performance and fast hashing.
- RandomPartitioner :
It is default partitioner prior to Cassandra 1.2. It distribute data over across cluster by using MD5 hash values.
- ByteOrderedPartitioner :
In Cassandra Query Language Byte Ordered partitioner data distribute over cluster based on data lexically by key bytes.It is used for ordered partitioning in Cassandra Query Language. It is also useful for backward compatibility.
Below given table is useful to understand, let’s have a look.
|Partitioner Name||Data Type||Distribute data over cluster based on|
|Murmur3Partitioner||bigint||MurmurHash hash values|
|RandomPartitioner||varint||MD5 hash values|
|ByteOrderedPartitioner||blob||data lexically by key bytes|
Let’s understand the TOKEN function using partitioning key and return query based on Token partitioning key. first we are creating table.
CREATE TABLE User_info ( Id int, Name text, Address text, PRIMARY KEY(Id, Name) );
To insert data into table User_info used the following CQL Query:
INSERT INTO User_info (Id, Name, Address) VALUES (301, 'Ashish', 'Delhi'); INSERT INTO User_info (Id, Name, Address) VALUES (302, 'Rana', 'Mumbai'); INSERT INTO User_info (Id, Name, Address) VALUES (303, 'Abi', 'Noida'); INSERT INTO User_info (Id, Name, Address) VALUES (302, 'me', 'Noida');
Let’s have a look.
SELECT * FROM User_info;
By using TOKEN function.
SELECT TOKEN(Id) FROM User_info;