3、Hive数据类型
第3章 Hive数据类型
3.1 基本数据类型

3.2 集合数据类型

例如:Json类型数据,Hive无法直接处理。需要在Hive建立对应表,并导入数据。

注:MAP,STRUCT,ARRAY的元素关系都可以用同一个字符表示:“_”
分析:
1、基于json数据结构,在hive创建对应表,导入数据。
create table test(
name string,
friends array<string>,
children map<string,int>,
address struct<street:string,city:string>
)
row format delimited
fields terminated by ','--列分隔符
collection items terminated by '_'--集合多个元素分割,MAP/STRUCT/ARRAY分隔符
map keys terminated by ':'--MAP中的key与value的分隔符
lines terminated by '\n'--默认
2、将数据写入文件
vi test.txt
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
3、加载数据
load data local inpath '/opt/module/data/test.txt' into table test;
4、查询数据:
hive>select friends[1],children['xiao song'],address.city from test where name="songsong";
3.3 类型转化
Hive的院子数据类型可隐式转换,类似于java的类型转换,例如某表达式使用Int类型,tinying会自动转换为int类型,但是hive不会反向转化。例如,某表达式使用tinyint类型,int不会自动转换为tinyint类型,会返回错误,除非使用CAST操作。
1、隐式转换规则:
- 任何整数类型都可以隐式转换为一个范围更广的类型:tinyint->int,int->bigint
- 所有整数类型、float、string类型都可以隐式转换成double
- tinyint、small、int可以转换为float
- boolean不可以转换任何其他类型
2、可以使用cast操作显示进行数据类型转换
cast('1' as int)--1
cast('z' as int)--null
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号