Cassandra Data Types | Built-in, Collection, User-defined
1. Objective
In the previous Cassandra article, we went through the CURD operations in Cassandra. In this article, we will study the Cassandra Data Types. Moreover, in CQL data types we will see 3 types of different types of data types Built-in, collection and user-defined data types.
So, let’s begin Cassandra Data Types.
2. Cassandra Data Types
Data Types usually signify the type of variables assigned in a program. We have studied many data types in other programming languages. For example int, char, float etc. In Cassandra query language, there are many data types. These Cassandra Data Types include built-in, collection and user-defined data types. The user can choose any of them as according the requirement of the program.
3. Types of Data Types in Cassandra
Following are the 3 Cassandra Data Types, let’s discuss them one by one:
a. Built-In Data Type
These Cassandra data types are basically pre-defined in Cassandra. The user can refer the variables to any of them. There are many built-in data types in Cassandra data types. Some of them are:
i. boolean
This Cassandra data type is used booleans. They represent two values, true or false. These are basically used for applications or variables involving just two values.
ii. blob
This Cassandra data type represents the arbitrary bytes. These arbitrary bytes include any kind of variable used to represent an arbitrary variable.
iii. ascii
This Cassandra data type is used for strings i.e. words and sentences. This represents the ASCII character string. The ASCII is basically an identifier for different characters. The value of the variable is stored as ASCII code of the value.
iv. bigint
This Cassandra data type is used for 64 bit signed long integer. This data type stores a higher range of integers as compared to int. In this data type 64 bit signed represents a value from -(2^32) to +2^32 roughly.
v. Counter
This Cassandra data type is used for integers. It represents a counter column. These columns are a part of the rows, which in turn are part of the column family. These contain numeric values, containing the number of columns.
vi. decimal
This data type is used for Example-integers. This is a basic data type used in approximately every language.
vii. double
It is used for integers. It represents a 64-bit floating point. These include a number with decimal points as well. Example 5.883, 94.2 etc.
viii. float
It is used for numbers. It represents a 32-bit floating point. This contains numbers in a lesser range than double. It represents values with a decimal point. Example 4.333, 45.23523 etc..
ix. inet
This is used to represent an IP address. The IP address of any system includes both numeric and characters. These numbers are not required in any arithmetic operation. Therefore, they are treated as a character. In other words, this data type is used for strings or characters.
x. int
They represent 32-bit signed integers. This is a low range version of big int. The signed integer represents numbers both negative and positive. The range of the number that int supports lie from -2^16 to 2^16 roughly.
xi. text
It is used for strings. It represents UTF8 encoded string. UTF 8 is a variable with character encoding. This basically encodes all the valid points in Uni-code using one to four 8-bit types.
xii. varchar
It is used for strings. It represents UTF8 encoded string. This basically describes a variable or arbitrary character. It is used to satisfy the UTF 8 as discussed in above data type
xiii. timestamp
It represents a timestamp. It consists of Cassandra collections of integers and strings both. This timestamp basically represents the time of creation. There are a lot of formats for the timestamp to be used. This format includes the date, year, month, time, and also the meridian position for some time.
The formats are:
The formats are:
yyyy-mm-dd HH:mm
yyyy-mm-dd HH:mm:ss
yyyy-mm-dd HH:mm:ss
Example: If the user wants time say, 15 February 1995, 02:15AM at GMT. The timestamp would be: 1995-02-15 02:15+0000.
All the meridian are represented as 4 digit number. For India, the number is +0530.
All the meridian are represented as 4 digit number. For India, the number is +0530.
xiv. variant
It is used for integers. It represents an arbitrary-precision integer.
Example-
After going through all the data types, we will go through an example covering all the data type. Let us make a table and illustrate the data types using this table.
- cqlsh:keyspace1>CREATE TABLE datatypes(
- type1 boolean,
- type2 blob,
- type3 ascii,
- type4 bigint,
- type5 counter,
- type6 decimal,
- type7 double,
- type8 float,
- type9 inet,
- type10 int,
- type11 <text, varchar>,
- type12 timestamp,
- type13 variant,
- );
Now, go through the table below to know about the data storage in the data types.
Name | Data type | Data stored |
Type1 | Boolean | True,false |
Type2 | Blob | //binary large objects |
Type3 | ASCII | 65(A), 97(a) etc |
Type4 | Bigint | -2^32 to +2^32 (only integers) |
Type5 | Counter | 1,2,3… |
Type6 | Decimal | 1,2,-2… |
Type7 | Double | -2^32 to +2^32 (all real numbers) |
Type8 | Float | -2^16 to +2^16 (all real numbers) |
Type9 | Inet | AXHFD327864SAF etc |
Type10 | Int | -2^16 to +2^16 (only integers) |
Type11 | Text, varchar | 79-*($wkjh#$ etc |
Type12 | Timestamp | 2018-05-18 03:21+0530 etc |
Type13 | Variant | 123,34,2213 etc |
b. Collection Data Type
This data type is used for collective data used in a single variable. Cassandra Query Language provides three type of collection data type.
i. List
This Cassandra data type represents a collection of one or more elements in a table.
ii. Map
This data type represents a collection of key-value pairs.
iii. Set
This Cassandra data type represents a collection of one or more sorted elements in a table.
c. User-Defined Data Type
CQL enables the user to create their own data type as per their requirement. After creating data type and fields in it the user can alter, verify and even drop a field or the whole data type.
So, this was all about Cassandra Data Types. Hope you like our explanation of data types in Cassandra CQL.
4. Conclusion
Hence, in this Cassandra Data Types tutorial, we studied 3 major data types: Built-in, Collection, and User-Defined Cassandra Data Types. Moreover, we saw many data types in Built-in Data Type. At last, we discussed Cassandra set data type, Cassandra map data type. In the next article, we will go through Collection Data Type in a detailed manner. Furthermore, for any query, feel free to ask through the comment section.
0 comments:
Post a Comment