Hive学习(三):Hive数据类型

数据类型

基本数据类型

这些数据类型都是基于Java的数据类型实现的,所以他们的行为表现和Java类型一致

类型 描述 boolean 布尔值 tinyint 1字节 有符号整数 smallint 2字节 int 4字节 bigint 8字节 float 4字节单精度浮点数 double 8字节双精度浮点数 deicimal 任意精度的带符号小数 string 变长字符串 varchar 变长字符串(1~65535) char 定长字符串(1-255) binary 字节数组 timestamp 时间戳,精度是纳秒(yyyy-mm-dd hh:mm:ss[.f...]) date 日期(YYYY-MM-DD)

复合数据类型

类型 描述 定义 array 有序集合,元素都是同类型的 array<数据类型> map 键值对 map<原始类型, 数据类型> struct 字段集合,类型可以不同 struct<列名 : 数据类型, ...> uniontype 不同类型的集合 uniontype<数据类型, 数据类型, ...>

其中uniontype在Hive的官方文档中表示对其支持是不完整的:

在join、where和group by子句中引用uniontype字段的查询将会失败,并且Hive没有定义语法来提取uniontype的标签或值字段。

使用例子

创建表

create table student(id bigint, name string, course array<string>, score map<string, int>, info struct<location:string, number:int>)
row format delimited
-- 字段用制表符分割
fields terminated by "	"
-- 集合元素用逗号分割
collection items terminated by ","
-- map键值用冒号分割
map keys terminated by ":"
lines terminated by "
";

导入数据

创建一个complex-student.txt文件,内容如下

1       张三    语文,数学,英语  a:68,b:80,c:90  xx小区,123123
2       李三    化学,物理,生物  d:68,e:80,f:90  xx公寓,321321

将数据导入student表

local代表后面的路径是我们服务器的路径,不写就是hdfs的文件系统路径

load data local inpath "/root/demo-apps/data-file/complex-student.txt" into table student;

查询数据

我们还能单独查询复合类型中的某一个值

select course[0], score[a], info.location from student;

另外,查看hdfs我们会发现,表中的数据实际就是以文件的方式存储的

[root@icydate data-file]# hadoop fs -ls hdfs://localhost:9000/user/hive/warehouse/test.db/student
2022-01-12 15:52:38,093 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 1 items
-rwxr-xr-x   1 root supergroup        122 2022-01-12 15:36 hdfs://localhost:9000/user/hive/warehouse/test.db/student/complex-student.txt
经验分享 程序员 微信小程序 职场和发展