Hive支持两种数据类型,一类叫原子数据类型,一类叫复杂数据类型。
1. 基本数据类型
hive不支持日期类型,在hive里日期都是用字符串来表示的,而常用的日期格式转化操作则是通过自定义函数进行操作。
2. 复杂数据类型
复杂数据类型包括数组(ARRAY)、映射(MAP)和结构体(STRUCT)
--数据展示 { "name":"zhangshan", "friends":["lisi","wangwu"], "children":{ "zhaoliu":18, "longqi":20 }, "address":{ "sreet":"hui long guan", "city":"beijing" } } --数据存储 zhangshan,lisi_wangwu,zhaoliu:18_longqi:20,hui long guan_beijing zhoushan,wanngsi_zhuwu,hangliu:20_longwu:30,tian tong yuan_beijing --创建表 create table if not exists kg_risk_person( name string, friends array<string>, children map<string,int>, address struct<sreet:string,city:string>) row format delimited fields terminated by "," collection items terminated by "_" map keys terminated by ":" lines terminated by "\n"; --上传数据 load data local inpath "/home/kg/qiaoruihua/hive/person.txt" into table kg_risk_person; --查询数据 select name,friends[0],children["zhaoliu"],address.city from kg_risk_person where name="zhangshan";
3. 数据类型转换
(1)隐式转换
任何类型都可以转换为范围更广的类型
整数类型,float,string都可以转换为double
tinyint,smallint,int都可以转换为float
boolen不可以转换为其他类型
(2)Cast显示转换
CAST("1" AS INT)
CAST("X" AS INT):强制转换失败,返回NULL