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

本文介绍了Hive中的数据类型,包括基本数据类型如布尔值、整数、浮点数等,以及复合数据类型如数组、映射和结构体。通过创建包含不同类型字段的表并导入示例数据,展示了如何在Hive中使用这些数据类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇:Hive学习(二):HiveQL DDL操作

下一篇:Hive学习(四):Hive文件存储结构

数据类型

基本数据类型

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

类型描述
boolean布尔值
tinyint1字节有符号整数
smallint2字节
int4字节
bigint8字节
float4字节单精度浮点数
double8字节双精度浮点数
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 "\t"
-- 集合元素用逗号分割
collection items terminated by ","
-- map键值用冒号分割
map keys terminated by ":"
lines terminated by "\n";

导入数据

创建一个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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值