n this post, I am sharing small demonstration to create CQL User Defined Data Type in Cassandra.
Using user defined data types, you can handle multiple fields into one user-defined column.
For example, You can create user-defined data type for Address related columns, in which you can specify a Street name, City name, Pincode, etc.
For example, You can create user-defined data type for Address related columns, in which you can specify a Street name, City name, Pincode, etc.
Create Keyspace if not exists:
1
|
CREATE KEYSPACE IF NOT EXISTS dbrnd WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
|
Use a Keyspace:
1
|
USE dbrnd;
|
Create user defined data type:
1
2
3
4
5
6
|
CREATE TYPE dbrnd.EmployeeAddress
(
StreetName TEXT
,CityName TEXT
,ZipCode INT
);
|
Create sample table:
frozenis require to define a column.
frozen
1
2
3
4
5
6
|
CREATE TABLE dbrnd.tbl_EmployeeInfo
(
EmpID INT PRIMARY KEY
,EmpName VARCHAR
,EmpAddress frozen<EmployeeAddress>
);
|
Insert sample records:
1
2
3
4
5
6
7
8
9
|
INSERT INTO dbrnd.tbl_EmployeeInfo
(EmpID,EmpName,EmpAddress)
VALUES
(1,'Anvesh',{StreetName:'Kondapuar', CityName:'Hyderabad', ZipCode:580084});
INSERT INTO dbrnd.tbl_EmployeeInfo
(EmpID,EmpName,EmpAddress)
VALUES
(2,'Neevan',{StreetName:'Hilldrive', CityName:'Bhavnagar', ZipCode:364002});
|
Select properties of User defined data type column:
1
2
3
4
5
6
7
8
9
10
11
|
cassandra@cqlsh:dbrnd> SELECT EmpAddress.StreetName FROM dbrnd.tbl_EmployeeInfo;
@ Row 1
-----------------------+-----------
empaddress.streetname | Kondapuar
@ Row 2
-----------------------+-----------
empaddress.streetname | Hilldrive
(2 rows)
|
0 comments:
Post a Comment