基本数据类型
Hive的数据类型
对于Hive的String类型相当于数据库的varchar类型,该类型是一个可变的字符串,不同于java的string类型(java的string类型是不可变字符串)

集合数据类型
Hive的数据类型
Hive中有三种复杂的数据类型array、map、和struct。array和map和jave中的array和map类似,而struct与java中的对象类似,它封装了一个命令字段集合,复杂数据类型允许任意层次的嵌套。

例子:
1)假设某表有如下一行,我们用JSON格式来表示其数据结构。在Hive下访问的格式为
{
“name”: “diaodiao”,
“friends”: [“bingbing” , “lili”] , //列表Array,
“children”: { //键值Map,
“xiao song”: 18 ,
“xiaoxiao song”: 19
}
“address”: { //结构Struct,
“street”: “hui long guan” ,
“city”: “beijing”
}
}
2)基于上述数据结构,我们在Hive里创建对应的表,并导入数据
创建本地文件/opt/module/datas/friend.txt
vim /opt/module/datas/friend
diaodiao,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing

注意:MAP,STRUCT和ARRAY里的元素间关系必须用同一个字符表示,这里用“”。
3)Hive上创建测试表
create table friend(name string,friends array,children map<string,int>,address structstreet:string,city:string
row format delimited fields terminated by ‘,’
collection items terminated by '

map keys terminated by ‘:’
lines terminated by ‘\n’;
4)导入文本到测试表
load data local inpath ‘/opt/module/datas/friend.txt’ into table friend;
5)访问三种集合列里面的数据
select friend[0],children[‘xiao song’],address.street from friend where name=“diaodiao”;

数据类型转换
1.隐式类型转换规则
(1)任何整数类型都可以隐式地转换为一个范围更广的类型,如TINYINT可以转换成INT,INT可以转换成BIGINT。
(2)所有整数类型、FLOAT和STRING类型都可以隐式地转换成DOUBLE。
(3)TINYINT、SMALLINT、INT都可以转换为FLOAT。
(4)BOOLEAN类型不可以转换为任何其它的类型。
2.可以使用CAST操作显示进行数据类型转换
例如CAST(‘1’ AS INT)将把字符串’1’ 转换成整数1;如果强制类型转换失败,如执行CAST(‘X’ AS INT),表达式返回空值 NULL。
0: jdbc:hive2://hadoop102:10000> select ‘1’+2, cast('1’as int) + 2;
±-----±-----±-+
| _c0 | _c1 |
±-----±-----±-+
| 3.0 | 3 |
±-----±-----±-+

相关文章:

  • 2021-07-13
  • 2021-05-17
猜你喜欢
  • 2021-04-06
  • 2022-02-22
  • 2022-12-23
  • 2021-11-05
  • 2021-06-23
相关资源
相似解决方案