Monday, January 15, 2018

UUID & TIMEUUID in Cassandra

1) Create a keyspace:

cassandra@cqlsh> CREATE KEYSPACE hireone WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1': '2', 'DC2': '1'}

2) Now create table with in the keyspace that was created above.

cassandra@cqlsh:hireone> desc table users;> CREATE TABLE users ( email text, last_login timeuuid, first_name text, last_name text, unique_id uuid primary key);

3) Describing the table that was created.
cassandra@cqlsh:hireone> desc table users;

CREATE TABLE hireone.users (
    unique_id uuid PRIMARY KEY,
    email text,
    first_name text,
    last_login timeuuid,
    last_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';

4) Insert values into the table.

cassandra@cqlsh:hireone> INSERT INTO users (unique_id, email, first_name, last_login, last_name) VALUES ( uuid(), 'brightside.pramod@gmail.com','pramod',now(),'p');

5) Check the output.

cassandra@cqlsh:hireone> select * from users;

 unique_id                            | email                       | first_name | last_login                           | last_name
--------------------------------------+-----------------------------+------------+--------------------------------------+-----------
 ac67790e-77f7-407a-a94f-d6a255f409f7 | brightside.pramod@gmail.com |     pramod | dd7783b0-fa7e-11e7-9375-f5ca3a099d88 |         p

Other view of above output, easy to view.

cassandra@cqlsh:hireone> expand on;
Now Expanded output is enabled
cassandra@cqlsh:hireone> select * from users;

@ Row 1
------------+--------------------------------------
 unique_id  | ac67790e-77f7-407a-a94f-d6a255f409f7
 email      | brightside.pramod@gmail.com
 first_name | pramod
 last_login | dd7783b0-fa7e-11e7-9375-f5ca3a099d88
 last_name  | p

(1 rows)
cassandra@cqlsh:hireone> expand off;
Disabled Expanded output.

6) Viewing the time from timeuuid column.

cassandra@cqlsh:hireone> select dateof(last_login) from users where unique_id = ac67790e-77f7-407a-a94f-d6a255f409f7;


 system.dateof(last_login)
---------------------------
  2018-01-16 05:34:04+0000

***

No comments:

Post a Comment