1. Overview
1.概述
In this tutorial, we’ll show some of the different data types of the Apache Cassandra database. Apache Cassandra supports a rich set of data types, including collection types, native types, tuple types, and user-defined types.
在本教程中,我们将展示Apache Cassandra数据库的一些不同数据类型。Apache Cassandra支持丰富的数据类型,包括集合类型、本地类型、元组类型和用户定义的类型。
The Cassandra Query Language (CQL) is a simple alternative to Structured Query Language (SQL). It is a declarative language developed to provide communication with its database. Similar to SQL, CQL also stores data in tables and organizes data into rows and columns.
Cassandra查询语言(CQL)是结构化查询语言(SQL)的一个简单替代方案。它是一种声明性语言,为提供与数据库的通信而开发。与SQL类似,CQL也将数据存储在表中,并将数据组织成行和列。
2. Cassandra Database Configuration
2.卡桑德拉数据库配置
Let’s create a database using a docker image and connect it to the database using cqlsh. Next, we should create a keyspace:
让我们使用docker图像创建一个数据库,并使用cqlsh连接到数据库。接下来,我们应该创建一个钥匙空间:。
CREATE KEYSPACE baeldung WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1};
For the purposes of this tutorial, we created a keyspace with only one copy of the data. Now, let’s connect the client session to a keyspace:
为了本教程的目的,我们创建了一个只有一份数据的钥匙空间。现在,让我们把客户会话连接到一个钥匙空间:。
USE <code class="language-shell">baeldung;
3. Built-in Data Types
3.内置数据类型
CQL supports a rich set of native data types. These data types come pre-defined, and we can directly refer to any of them.
CQL支持一组丰富的本地数据类型。这些数据类型是预先定义的,我们可以直接引用其中的任何一种。
3.1. Numeric Types
3.1.数值类型
Numeric types are similar to standard types in Java and other languages such as integers or floating-point numbers with different ranges:
数字类型类似于Java和其他语言中的标准类型,如整数或浮点数,但范围不同。
Let’s create a table with all these data types:
让我们用所有这些数据类型创建一个表。
CREATE TABLE numeric_types
(
type1 int PRIMARY KEY,
type2 bigint,
type3 smallint,
type4 tinyint,
type5 varint,
type6 float,
type7 double,
type8 decimal
);
3.2. Text Types
3.2 文本类型
CQL provides two data types for representing text. We can use text or varchar to create a UTF-8 character string. UTF-8 is the more recent and widely used text standard and supports internationalization.
CQL提供两种数据类型来表示文本。我们可以使用text或varchar来创建一个UTF-8字符串。UTF-8是较新和广泛使用的文本标准,并支持国际化。
There is also the ascii type to create an ASCII character string. The ascii type is most useful if we are dealing with legacy data that is in ASCII format. The size of the text values is limited by the maximum size of the column. The single-column value size is 2 GB, but recommended is only 1 MB.
还有一种ascii类型,用于创建ASCII字符串。如果我们处理的是ASCII格式的遗留数据,ascii类型是最有用的。文本值的大小受到列的最大尺寸的限制。单列值的大小是2GB,但推荐的是只有1MB。
Let’s create a table with all these data types:
让我们用所有这些数据类型创建一个表。
CREATE TABLE text_types
(
primaryKey int PRIMARY KEY,
type2 text,
type3 varchar,
type4 ascii
);
3.3. Date Types
3.3 日期类型
Now, let’s talk about date types. Cassandra provides several types which prove quite useful in defining unique partition keys or define ordinary columns:
现在,让我们来谈谈日期类型。Cassandra提供了几种类型,证明在定义唯一分区键或定义普通列时相当有用。
time did is represented by UUID version 1. We can input integer or string to CQL timestamp, time, and date. Values of the duration type are encoded as 3 signed integers.
时间是由UID版本1表示的。 我们可以向CQL时间戳、时间和日期输入整数或字符串。持续时间类型的值被编码为3个有符号的整数。
The first integer represents the number of months, the second the number of days, and the third the number of nanoseconds.
第一个整数代表月数,第二个整数代表日数,第三个整数代表纳秒数。
Let’s see an example of create table command:
让我们看看创建表命令的一个例子。
CREATE TABLE date_types
(
primaryKey int PRIMARY KEY,
type1 timestamp,
type2 time,
type3 date,
type4 timeuuid,
type5 duration
);
3.4. Counter Type
3.4.计数器类型
The counter type is used to define counter columns. A counter column is a column whose value is a 64-bit signed integer. We can only perform two operations on the counter column – incrementing and decrementing.
计数器类型用于定义计数器列。计数器列是一个列,其值是一个64位有符号的整数。我们只能对计数器列进行两种操作–递增和递减。
Therefore, we can’t set the value to the counter. We can use counters for tracking statistics such as numbers of page views, tweets, log messages, and so on. We can’t mix the counter type with other types.
因此,我们不能给计数器设置值。我们可以使用计数器来跟踪统计数据,如页面浏览量、推文、日志信息等的数量。我们不能将计数器类型与其他类型混合使用。
Let’s see an example:
让我们看一个例子。
CREATE TABLE counter_type
(
primaryKey uuid PRIMARY KEY,
type1 counter
);
3.5. Other Data Types
3.5.其他数据类型
- boolean is a simple true/false value
- uuid is a Type 4 UUID, which is based entirely on random numbers. We can input UUIDs by using dash-separated sequences of hex digits
- A binary large object (blob) is a colloquial computing term for an arbitrary array of bytes. The CQL blob type stores media or other binary file types. The maximum blob size is 2 GB, but less than 1 MB is recommended.
- inet is the type that represents IPv4 or IPv6 Internet addresses
Again, let’s create a table with these types:
同样,让我们用这些类型创建一个表。
CREATE TABLE other_types
(
primaryKey int PRIMARY KEY,
type1 boolean,
type2 uuid,
type3 blob,
type4 inet
);
4. Collection Data Types
4.采集数据类型
Sometimes we want to store data of the same type without generating new columns. Collections can store multiple values. CQL provides three collection types to help us such as lists, sets, and maps.
有时我们想在不生成新列的情况下存储相同类型的数据。集合可以存储多个值。CQL提供了三种集合类型来帮助我们,如列表、集合和地图。
For instance, we can create a table having a list of textual elements, a list of integers, or a list of some other element types.
例如,我们可以创建一个具有文本元素列表、整数列表或其他一些元素类型列表的表。
4.1. Set
4.1.设置
We can store multiple unique values using the set data type. Likewise, in Java, the elements are not stored in order.
我们可以使用集合数据类型来存储多个唯一的值。同样地,在Java中,元素不是按顺序存储的。
Let’s create a set:
让我们创建一个集合。
CREATE TABLE collection_types
(
primaryKey int PRIMARY KEY,
email set<text>
);
4.2. List
4.2.列表
In this data type, the values are stored in the form of a list. We can’t change the order of the elements. After storing the values in the list, the elements get a particular index. We can retrieve data by using these indexes.
在这种数据类型中,数值是以列表的形式存储的。我们不能改变元素的顺序。在列表中存储数值后,这些元素得到一个特定的索引。我们可以通过使用这些索引来检索数据。
Unlike sets, lists can store duplicate values. Let’s add a list to our table:
与集合不同,列表可以存储重复的值。让我们在我们的表中添加一个列表。
ALTER TABLE collection_types
ADD scores list<text>;
4.3. Map
4.3.地图
Using Cassandra, we can store data in sets of key-value pairs using the map data type. Keys are unique. Because of that, we can sort maps by their keys.
使用Cassandra,我们可以使用map数据类型将数据存储在键值对的集合中。键是唯一的。正因为如此,我们可以按键对地图进行排序。
Let’s add another column to our table:
让我们为我们的表格添加另一列。
ALTER TABLE collection_types
ADD address map<uuid, text>;
5. Tuples
5.图元
Tuples are a set of different types of elements. These sets have a fixed length:
图元是不同类型元素的集合。这些集合有一个固定的长度。
CREATE TABLE tuple_type
(
primaryKey int PRIMARY KEY,
type1 tuple<int, text, float>
);
6. User-Defined Data Types
6.用户定义的数据类型
Cassandra provides the possibility for creating our own data types. We can create, modify and remove these data types. Firstly, let’s create our own type:
Cassandra提供了创建我们自己的数据类型的可能性。我们可以创建、修改和删除这些数据类型。首先,让我们来创建我们自己的类型。
CREATE TYPE user_defined_type (
type1 timestamp,
type2 text,
type3 text,
type4 text);
So, now we can create a table with our type:
所以,现在我们可以用我们的类型创建一个表。
CREATE TABLE user_type
(
primaryKey int PRIMARY KEY,
our_type user_defined_type
);
7. Conclusion
7.结语
In this quick tutorial, we explored the basic CQL data types. In addition, we created tables with these data types. After that, we talked about what kind of data they can store.
在这个快速教程中,我们探讨了基本的CQL数据类型。此外,我们用这些数据类型创建了表。之后,我们谈到了它们可以存储什么样的数据。
As always, the full source code of the article is available over on GitHub.
一如既往,该文章的完整源代码可在GitHub上获得。