User Defined Type in Cassandra

Category: Cassandra   Tags: Cassandra, Learning, Beginners, Basics, NoSQL Database, Cassandra CQL, User Defined Type in Cassandra

A user-defined type helps handling multiple fields of related information in a table. Multiple tables can be simplified to use fewer tables by using a user-defined type to represent the related fields of information. Here we will show you how to create and use user-defined types:

Use CREATE TYPE command to create a user defined type:

CREATE TYPE user_basic_info (
  birthday timestamp,
  nationality text,
  city text
);
                              

Now create table with this user defined type:

CREATE TABLE users (
  id text PRIMARY KEY,
  name text,
  basics FROZEN<user_basic_info>
);
                              

Insert data to users table:

INSERT INTO users (id, name, basics) VALUES (
  '123', 'Abhay', { birthday : '1997-02-01', nationality : 'Indian', city : 'Delhi' });