Hive学习(三):Hive数据类型
数据类型
基本数据类型
这些数据类型都是基于Java的数据类型实现的,所以他们的行为表现和Java类型一致
复合数据类型
其中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