Cassandra Data Types
Cassandra is a distributed NoSQL database that allows for flexible and scalable data modeling. One of the key features of Cassandra is its support for a wide range of data types that can be used to model various types of data structures. In this tutorial, we will discuss the different data types supported by Cassandra and provide examples of their usage.
Data Types in Cassandra
Cassandra supports several data types, including:
- Text: This data type is used to store variable-length character strings. It supports up to 2 billion characters.
- ASCII: This data type is used to store 7-bit ASCII character strings.
- Integer: This data type is used to store signed 32-bit integers.
- BigInt: This data type is used to store signed 64-bit integers.
- Float: This data type is used to store single-precision 32-bit floating-point numbers.
- Double: This data type is used to store double-precision 64-bit floating-point numbers.
- Boolean: This data type is used to store true/false values.
- Timestamp: This data type is used to store timestamps. It supports a range from 0001-01-01 to 9999-12-31.
- UUID: This data type is used to store universally unique identifiers (UUIDs). UUIDs are 128-bit values that are unique across time and space.
- TimeUUID: This data type is used to store version 1 UUIDs, which are based on timestamps.
- Inet: This data type is used to store IPv4 or IPv6 addresses.
- Varint: This data type is used to store arbitrarily large integers.
- Decimal: This data type is used to store arbitrary-precision decimal numbers.
- Blob: This data type is used to store arbitrary binary data.
Examples of Data Types in Cassandra
Let’s look at some examples of using the different data types in Cassandra.
Example 1: Using the Text Data Type
CREATE TABLE users ( id UUID PRIMARY KEY, name TEXT, email TEXT, created_at TIMESTAMP );
This example creates a table named users with four columns: id, name, email, and created_at. The name and email columns are of the TEXT data type, which allows for variable-length character strings.
Example 2: Inserting data into the table
INSERT INTO users (id, name, email, created_at) VALUES (550e8400-e29b-41d4-a716-446655440000, 'John Doe', '[email protected]', 1495473283145);
This example inserts a row into the users table with the values for each column specified. The created_at column is of the TIMESTAMP data type.
Example 3: Retrieving data from the table
SELECT * FROM users;
This example retrieves all rows from the users table.
Example 4: Using the List Data Type
