- 通过思维导图来分类和理解:
- 下面通过列表说明所有的基本数据类型:
| 类型 | 描述 | 示例 |
| — | — | — |
| BOOLEAN | true/false | TRUE |
| TINYINT | 1字节有符号整数,从-128到127 | 1Y |
| SMALLINT | 2字节有符号整数,从-32768到32767 | 1S |
| INT | 4字节有符号整数,从-2147483648到2147483647 | 1 |
| BIGINT | 8字节有符号整数,
从-9223372036854775808到9223372036854
【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
775807 | 1L |
| FLOAT | 4字节单精度浮点数 | 1.0 |
| DOUBLE | 8字节(64位)双精度浮点数 | 1.0 |
| DECIMAL | 任意精度有符号小数 | 1.0 |
| STRING | 无上限可变长度字符串 | ‘a’, “a” |
| VARCHAR | 可变长度字符串 | ‘a’, “a” |
| CHAR | 固定长度字符串 | ‘a’, “a” |
| BINARY | 字节数组 | - |
| TIMESTAMP | 精度到纳秒的时间戳 | ‘2020-03-01 00:00:00’ |
| DATE | 日期 | ‘2020-01-01’ |
关于三种字符串类型
-
STRING,变长,无需最大长度声明,理论上能存储2GB字符;
-
VARCHAR,变长,需要声明最大长度(1到65535之间),例如VARCHAR(100);
-
CHAR,定长,如CHAR(100);
常用类型综合实战
- 启动hive,进入交互模式,执行以下语句建表,字段类型是常用的几种:
create table t1(
int_field INT,
string_field STRING,
boolean_field BOOLEAN,
date_field DATE,
timestamp_field TIMESTAMP
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t’
STORED AS TEXTFILE;
- 用insert语句新增一条记录:
insert into t1
values
(101, ‘a101’, true, ‘2020-01-01’, ‘2020-03-01 00:00:00’);
- 使用函数,在新增的记录的时候,将timestamp_field字段设置为当前时间,注意和前面的insert语句比较一下,使用函数时必须用到select XXX from XXX limit 1这样的语法:
insert into t1
select
103, ‘a103’, true, ‘2020-01-01’, current_timestamp()
from t1 limit 1;
- 看一下新增的两条记录:
hive> select * from t1;
OK
101 a101 true 2020-01-01 2020-03-01 00:00:00
103 a103 true 2020-01-01 2020-10-27 20:42:35.729
Time taken: 0.062 seconds, Fetched: 2 row(s)
- 查看当前时间:
select unix_timestamp();
响应:
hive> select unix_timestamp();
OK
1603802631
Time taken: 0.028 seconds, Fetched: 1 row(s)
- 将DATE字段转为TIMESTAMP:
select cast(date_field as timestamp) from t1;
响应:
hive> select cast(date_field as timestamp) from t1;
OK
2020-01-01 00:00:00
2020-01-01 00:00:00
Time taken: 0.051 seconds, Fetched: 2 row(s)
- 将TIMESTAMP字段转为DATE:
select to_date(timestamp_field) from t1;
响应: