Thursday 5 March 2020

Cassandra: Working with user defined types

In this tutorial series, I am going to explain how to work with user defined types.

You are going to learn,
a.   How to create user defined type
b.   How to use user defined type
c.    How to alter user defined type
d.   How to drop user defined type

How to create user defined type?
Using 'CREATE TYPE' statement, you can create user defined types.

Syntax
<create-type-stmt> ::= CREATE TYPE ( IF NOT EXISTS )? <typename>
                         '(' <field-definition> ( ',' <field-definition> )* ')'

<typename> ::= ( <keyspace-name> '.' )? <identifier>

<field-definition> ::= <identifier> <type>

Example
CREATE TYPE cassandratutorial.address (
 street_name text,
    street_number int,
    city text,
    state text,
    country text,
    zip int
)

CQL Script
CREATE KEYSPACE cassandratutorial WITH REPLICATION = 
{ 
  'class' : 'SimpleStrategy', 
  'replication_factor' : 1 
};

CREATE TYPE cassandratutorial.address (
 street_name text,
    street_number int,
    city text,
    state text,
    country text,
    zip int
);

CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
  id INT PRIMARY KEY, 
  firstName VARCHAR,
  lastName VARCHAR,
  addressInfo address,
  age int
);

INSERT INTO cassandratutorial.employee JSON '{"id" : 1, "firstName" : "Krishna", "lastName" : "Gurram", "age" : 30, "addressInfo" : {"street_name" : "Marthalli", "street_number" : 123, "city" : "Bangalore", "state" : "Karnataka", "country" : "India", "zip" : 560037}}';

Cqlsh console output

cqlsh> CREATE KEYSPACE cassandratutorial WITH REPLICATION = 
   ... { 
   ...   'class' : 'SimpleStrategy', 
   ...   'replication_factor' : 1 
   ... };
cqlsh> 
cqlsh> CREATE TYPE cassandratutorial.address (
   ... street_name text,
   ...     street_number int,
   ...     city text,
   ...     state text,
   ...     country text,
   ...     zip int
   ... );
cqlsh> 
cqlsh> CREATE TABLE IF NOT EXISTS cassandratutorial.employee (
   ...   id INT PRIMARY KEY, 
   ...   firstName VARCHAR,
   ...   lastName VARCHAR,
   ...   addressInfo address,
   ...   age int
   ... );
cqlsh> 
cqlsh> INSERT INTO cassandratutorial.employee JSON '{"id" : 1, "firstName" : "Krishna", "lastName" : "Gurram", "age" : 30, "addressInfo" : {"street_name" : "Marthalli", "street_number" : 123, "city" : "Bangalore", "state" : "Karnataka", "country" : "India", "zip" : 560037}}';
cqlsh> 
cqlsh> SELECT * FROM cassandratutorial.employee;

 id | addressinfo                                                                                                          | age | firstname | lastname
----+----------------------------------------------------------------------------------------------------------------------+-----+-----------+----------
  1 | {street_name: 'Marthalli', street_number: 123, city: 'Bangalore', state: 'Karnataka', country: 'India', zip: 560037} |  30 |   Krishna |   Gurram

(1 rows)




Previous                                                    Next                                                    Home

No comments:

Post a Comment