全网最细最全OLAP之clickhouse笔记|clickhouse文档|clickhouse揭秘文档(四)--clickhouse的数据类型

1 数据类型

在建表的时候指定字段的数据类型, 数据类型在使用的时候是区分大小写的 ,所以在定义字段的时候一定注意数据类型的书写

1.1 整数数据类型

Int Ranges 

  • Int8 - [-128 : 127]
  • Int16 - [-32768 : 32767]
  • Int32 - [-2147483648 : 2147483647]
  • Int64 - [-9223372036854775808 : 9223372036854775807]

Uint Ranges 

  • UInt8 - [0 : 255]
  • UInt16 - [0 : 65535]
  • UInt32 - [0 : 4294967295]
  • UInt64 - [0 : 18446744073709551615]

例如:

create table tb_name(
id UInt8 , -- 指定数据类型
age UInt8 , -- 指定数据类型
flow Int64 -- 指定数据类型
) engine=Log ; -- 指定表引擎 后面章节详细介绍 

1.2 小数

  • Float32 - float
  • Float64 - double
  • Decimal(P,S) Decimal32(s) Decimal64(s) ) Decimal128(s)

例子

create table tb_user(
uid Int8 ,
sal Decimal32(2) -- 指定2位小数点
) engine=TinyLog ;
insert into tb_user values(1,10000),(2,30000),(3,2000) ;
SELECT *
FROM tb_user

┌─uid─┬──────sal─┐
│   1 │ 10000.00 │
│   2 │ 30000.00 │
│   3 │  2000.00 │
└─────┴──────────┘

1.3 boolean类型

CH中没有对布尔类型进行支持,可以使用0 和1 来代表布尔数据类型

1.4 字符串数据类型

  • String                  -- 字符串数据类型   一般情况下使用String;类型就可以
  • FixedString(n)     -- 固定长度的数据类型
drop table if exists tb_stu ;
create table if not exists tb_stu(
sid FixedString(8) ,
name String ,
address String
) engine = TinyLog ;

insert into tb_stu values('aaaaaaaa' , 'HANGGE' ,'ZhongNanHai') ;

1.5  UUID

随机的一串字符串

create table tb_uuid(
id UUID ,
name String
) engine = TinyLog ;

insert into tb_uuid (name) values ('zss') , ('lss') ,('daa') ;
-- 使用参入语法生成一个随机数参入到表中
insert into tb_uuid select generateUUIDv4() , 'HANGGE' ;
┌───────────────────────────────────id─┬─name───┐
│ 00000000-0000-0000-0000-000000000000 │ zss    │
│ 00000000-0000-0000-0000-000000000000 │ lss    │
│ 00000000-0000-0000-0000-000000000000 │ daa    │
│ 1e3a7f12-f49b-4ffa-8308-945d6e520ca4 │ HANGGE │
└──────────────────────────────────────┴────────┘

1.6 枚举类型

包括 Enum8 和 Enum16 类型。Enum 保存 'string'= integer 的对应关系。
Enum8 用 'String'= Int8 对描述。
Enum16 用 'String'= Int16 对描述。
用法演示:
创建一个带有一个枚举 Enum8('hello' = 1, 'world' = 2) 类型的列:


CREATE TABLE t_enum
(
    x Enum8('hello' = 1, 'world' = 2)
)
ENGINE = TinyLog
这个 x 列只能存储类型定义中列出的值:'hello'或'world'。如果尝试保存任何其他值,ClickHouse 抛出异常。
:) INSERT INTO t_enum VALUES ('hello'), ('world'), ('hello')

INSERT INTO t_enum VALUES

Ok.

3 rows in set. Elapsed: 0.002 sec.

:) insert into t_enum values('a')

INSERT INTO t_enum VALUES


Exception on client:
Code: 49. DB::Exception: Unknown element 'a' for type Enum8('hello' = 1, 'world' = 2)
从表中查询数据时,ClickHouse 从 Enum 中输出字符串值。
SELECT * FROM t_enum

┌─x─────┐
│ hello │
│ world │
│ hello │
└───────┘
如果需要看到对应行的数值,则必须将 Enum 值转换为整数类型。
SELECT CAST(x, 'Int8') FROM t_enum

┌─CAST(x, 'Int8')─┐
│               1 │
│               2 │
│               1 │
└─────────────────┘

cast 强制数据类型转换... 将枚举类型字段转换成Int8数据类型

1.7 数组

Array(T)由 T 类型元素组成的数组。

T 可以是任意类型,包含数组类型。 但不推荐使用多维数组,ClickHouse 对多维数组的支持有限。例如,不能在 MergeTree 表中存储多维数组。

可以使用array函数来创建数组:

  • 1) array(T)
  • 2) []
:) SELECT array(1, 2) AS x, toTypeName(x)

SELECT
    [1, 2] AS x,
    toTypeName(x)

┌─x─────┬─toTypeName(array(1, 2))─┐
│ [1,2] │ Array(UInt8)            │
└───────┴─────────────────────────┘

1 rows in set. Elapsed: 0.002 sec.

:) SELECT [1, 2] AS x, toTypeName(x)

SELECT
    [1, 2] AS x,
    toTypeName(x)

┌─x─────┬─toTypeName([1, 2])─┐
│ [1,2] │ Array(UInt8)       │
└───────┴────────────────────┘

-- 数组的取值

SELECT ['a', 'b', 'c'][1]

┌─arrayElement(array('a', 'b', 'c'), 1)─┐
│ a                                     │
└───────────────────────────────────────┘

1 rows in set. Elapsed: 0.002 sec. 

linux01 :) select array('a','b','c')[2];

SELECT ['a', 'b', 'c'][2]

┌─arrayElement(array('a', 'b', 'c'), 2)─┐
│ b                                     │
└───────────────────────────────────────┘

1.8 Nested数据类型

Nested(name1 Type1, Name2 Type2, …)

CREATE TABLE tb_nested
(
    `id` String,
    `user` Nested(    uid Int,     name String,     age UInt8)
)
ENGINE = TinyLog

Ok.

0 rows in set. Elapsed: 0.008 sec. 

linux01 :) desc tb_nested ;

DESCRIBE TABLE tb_nested

┌─name──────┬─type──────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id        │ String        │              │                    │         │                  │                │
│ user.uid  │ Array(Int32)  │              │                    │         │                  │                │
│ user.name │ Array(String) │              │                    │         │                  │                │
│ user.age  │ Array(UInt8)  │              │                    │         │                  │                │
└───────────┴───────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘

插入数据

insert into tb_nested values(1 , [1,2,3],['zss','lss','ymm'],[21,33,18]) ;


┌─id─┬─user.uid─┬─user.name───────────┬─user.age───┐
│ 1  │ [1,2,3]  │ ['zss','lss','ymm'] │ [21,33,18] │
└────┴──────────┴─────────────────────┴────────────┘

查询数据

SELECT 
    user.uid,
    user.name
FROM tb_nested

┌─user.uid─┬─user.name───────────┐
│ [1,2,3]  │ ['zss','lss','ymm'] │
└──────────┴─────────────────────┘
SELECT 
    arrayJoin(user.uid),
    arrayJoin(user.name)
FROM tb_nested

┌─arrayJoin(user.uid)─┬─arrayJoin(user.name)─┐
│                   1 │ zss                  │
│                   1 │ lss                  │
│                   1 │ ymm                  │
│                   2 │ zss                  │
│                   2 │ lss                  │
│                   2 │ ymm                  │
│                   3 │ zss                  │
│                   3 │ lss                  │
│                   3 │ ymm                  │
└─────────────────────┴──────────────────────┘

1.9 元组

Tuple(T1, T2, ...)元组,其中每个元素都有单独的类型。

创建元组的示例:

:) SELECT tuple(1,'a') AS x, toTypeName(x)



SELECT

    (1, 'a') AS x,

    toTypeName(x)



┌─x───────┬─toTypeName(tuple(1, 'a'))─┐

│ (1,'a') │ Tuple(UInt8, String)      │

└─────────┴───────────────────────────┘



1 rows in set. Elapsed: 0.021 sec.

还有一些其他数据类型 , 在后面的案例中使用的时候再做介绍

猜你喜欢

转载自blog.csdn.net/qq_37933018/article/details/108022318