Cassandra User Defined Types- Create, Alter, Drop, Describe
1. Objective
In our previous Cassandra tutorial, we studied about data types used in Cassandra. In this article, we will know the Cassandra User Defined Types. While using a User Defined Type in Cassandra, we will also look at create, drop, alter and describe the function. Moreover, we will discuss Cassandra user-defined types of performance.
So, let’s discuss Cassandra User Defined Types in detail.
2. Cassandra User Defined Types
As the name suggests, the user has the ability to define certain data types. Cassandra Query Language allows the user to do so. The user may create, alter, drop or describe a data type. The data types can be according to the user’s requirement as according to the program or field. Let’s discuss it more.
a. Create
CQL allows the user to create data types. The user can create the data type using this syntax:
- CREATE TYPE <keyspace>.<data type>
- (variable,variable).
Here, the user has to make sure that the name used for user-defined data type and predefined data type are not the same.
Let’s create a cassandra user-defined data type. In the example, we will create ‘records’ for college students with following field name and field type.
Table.1 Cassandra User Defined Types – Create
FIELD NAME | FIELD TYPE | DESCRIPTION |
Name | Text | Name of the student. |
Branch | Text | Major of the degree. |
Phone | Int | Phone number. |
City | Text | City they are from. |
Id | int | Enrolment number |
Example 1: Creating the data type.
Input:
- cqlsh:keyspace1> CREATE TYPE records (
- ... name text,
- ... branch text,
- ... phone int,
- ... city text,
- ... id set <int>,
- ... );
b. Alter
After creating the data type in cassandra, a user also has an option to alter the contents. The user can add and rename fields of the data type.
To add a field in a data type, the user can use ALTER TYPE along with ADD command. The syntax is
- ALTER TYPE <data type>
- ADD <field name><field type>;
Example 2: Adding field email to the data type.
Input:
- ALTER TYPE records
- ADD email text;
If the user wants to rename a field, he can use ALTER TYPE along with RENAME command. The syntax to rename is.
- ALTER TYPE<data type>
- RENAME<existing name>TO<new name>;
Example 3: Changing the name of field ‘id’ to ‘en’.
Input:
- ALTER TYPE records
- RENAME id TO en;
c. Drop
Using DROP TYPE command user can drop or delete a Cassandra user-defined data type. The user can use syntax given below.
- cqlsh:<keyspace>>DROP TYPE <data type>
Example 4: Dropping the field ‘city’.
Input:
- cqlsh:keyspace1> DROP TYPE city;
d. Describe
The user can verify the contents of the data type by using DESCRIBE TYPE command. The syntax used for this is.
- cqlsh:<keyspace>> DESCRIBE TYPE<data type>;
Input:
- cqlsh:keyspace1> DESCRIBE TYPE records;
Output after Example 1:
CREATE TYPE keyspace1.records(
… name text,
… branch text,
… phone int,
… city text,
… id set <int>,
… );
OUTPUT after Example 2:
CREATE TYPE keyspace1.records(
… name text,
… branch text,
… phone int,
… city text,
… id set <int>,
… email text,
… );
OUTPUT after Example 3:
CREATE TYPE keyspace1.records(
… name text,
… branch text,
… phone int,
… city text,
… en set <int>,
… email text,
… );
OUTPUT after Example 4:
CREATE TYPE keyspace1.records(
… name text,
… branch text,
… phone int,
… en set <int>,
… email text,
… );
So, this was all about Cassandra User Defined Types. Hope You like our explanation.
3. Conclusion
Hence, in this article, we got to know about the Cassandra User Defined Data Types. Moreover, we also studied create, alter, drop and describe the user-defined types of Cassandra. In the next article, we will study about troubleshooting in Cassandra. Furthermore, if you have any query regarding Cassandra user defined types, feel free to ask through the comment section.
0 comments:
Post a Comment