Overview of User Defined Type (UDT) in Cassandra

30 Dec, 2019
User Defined Data Type:One of the advantages of User Defined Data(UDT) is to Attach multiple data fields to a column. In Cassandra, UDTs play a vital role which allows group related fields (such that field 1, field 2, etc.) can be of data together and are named and type.
In Cassandra one of the advantage of UDTs which helps to add flexibility to your table and data model. we can construct UDT provided by Cassandra: UDT, which stands for User-Defined Type. As we can show in the example that User-defined types (UDTs) can attach multiple data fields in which each named and typed can be mapped to a single column.
To construct User Defined Type (UDT) any valid data type can be used for fields type in Cassandra, including collection ( SET, LIST, MAP) or any other UDTs. Once a UDT in Cassandra is created then it can be used to define a column in the main table.
Syntax to define UDT:
CREATE TYPE UDT_name 
(
  field_name 1 Data_Type 1,
  field_name 2 Data_Type 2,
  field_name 3 Data_Type 3,
  field_name 4 Data_Type 4,
  field_name 5 Data Type 5,
);

Simple steps to create UDTs:
Step-1: Create a KEYSPACE, If not existed. Syntax:
create_keyspace_statement ::=  
      CREATE KEYSPACE [ IF NOT EXISTS ] keyspace_name 
       WITH options 
Step-2: To create keyspace used the following CQL query.
CREATE KEYSPACE Emp
    WITH replication = {'class': 'SimpleStrategy', 
                        'replication_factor' : 1};  
To check keyspace Schema used the following CQl query.
DESCRIBE KEYSPACE Emp;
Step-3: To Create Employee User Data Type for Current address used the following CQL query. For example, User Data Type for Current address
CREATE TYPE Emp.Current_add
(
Emp_id int,
h_no text,
city text,
state text,
pin_code int,
country text 
); 
Here Current_add is a Cassandra user-defined data type.
Figure – User-defined types (UDTs)
Step-4: Create table Registration that is having current_address UDT as one of the column, for example:
CREATE TABLE Registration
( 
Emp_id int PRIMARY KEY, 
Emp_Name text, 
current_address FROZEN<Current_add>
); 

Step-5: To insert data using UDT in Cassandra used the following CQL query.
Format-1: using JSON (JavaScript Object Notation) format.In Cassandra we can also insert data in JSON (JavaScript Object Notation) format. For example: CQL query by using JSON format.
INSERT INTO Registration JSON'
{
"Emp_id"            : 2000, 
"current_address"   :  { "h_no" : "A 210", "city" : 
                        "delhi", "state" : "DL",
                        "pin_code" :"201307", 
                        "country" :"india"},
"Emp_Name"          : "Ashish Rana"}' ;
 
Format-2: simple insertion
INSERT INTO Registration (Emp_id, Emp_Name, current_address ) 
            values (1000, 'Ashish', { h_no :'A 210', city : 'delhi', state : 'DL', pin_code 
                                    :12345, country :'IN'});

INSERT INTO Registration(Emp_id, Emp_Name, current_address ) 
            values (1001, 'kartikey Rana', { h_no : 'B 210 ',  city 
                                    : 'mumbai', state : 'MH', pin_code 
                                    :12345, country :'IN'});

INSERT INTO Registration(Emp_id, Emp_Name, current_address ) 
            values (1002, 'Dhruv Gupta', { h_no : 'C 210', city 
                                    : 'delhi', state : 'DL', pin_code 
                                    :12345, country :'IN'}); 

Output:
Figure – UDT Table-insertion
In this case, we will see how to insert command without inserting the value of one or more fields.For example, we are not passing the value of the field here. How Cassandra will handle this?Well, it will insert this value as a normal value but it will take the field value is null. Every field value, except the primary key that we do not pass at the time of insertion, Cassandra will take it as null.
CQL query without insert one field or more field value of the UDT:
INSERT INTO Registration(Emp_id, Emp_Name, current_address ) 
                 values (1003, 'Amit Gupta', { h_no : 'D 210',  city 
                               : 'Bangalore', state : 'MH', pin_code :12345}
                               );

INSERT INTO Registration(Emp_id, Emp_Name, current_address ) 
                 values (1004, 'Shivang Rana', { h_no : 'E 210', city 
                               : 'Hyderabad', state : 'HYD'});
                               

Output:
Figure – UDT table without insert one or more field value
Ashish_rana
Apache
DBMS
Technical Scripter
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
File Organization in DBMS | Set 1
File Organization in DBMS | Set 2
Introduction of DBMS (Database Management System) | Set 1
Difference between DELETE, DROP and TRUNCATE
File Organization in DBMS | Set 3
Record-Based Data Model
Introduction of B+ Tree
Difference between Primary Key and Foreign Key
Definition and Overview of ODBMS
SQL | Query Processing