hive基本类型和复杂类型



hive的数据类型


1、基本类型

create table student (id int, name string, sex boolean, zipcode string);
HQL语句会被翻译成MR程序的。 这些类型底层其实也会自动的被包装成可序列化的类型


八大基本类型 + String
int    string   float,  double  boolean, 
date   timestamp


复杂类型:
Array : 该集合中的所有元素的类型和意义都一致 (指定元素的类型)
array(key,value1,key2,value2,....)
Map : 键值对的keyvalue类型,泛型
Struct : address struct<int, string, string ,float, boolean>


union : 解决以上三种复杂类型的嵌套使用问题


2、复杂类型



例子程序:


CREATE TABLE student_c(
name STRING, favors ARRAY<STRING>, scores MAP<STRING, FLOAT>, 
address STRUCT<province:STRING, city:STRING, detail:STRING, zip:INT>
) ROW FORMAT DELIMITED
 FIELDS TERMINATED BY '\t' 
 COLLECTION ITEMS TERMINATED BY ';' 
 MAP KEYS TERMINATED BY ':' ;




 2.1 array的使用 


准备的数据: 在 array.txt
huangbo beijing,shanghai,tianjin,hangzhou
xuzheng changchu,chengdu,wuhan
wangbaoqiang    dalian,shenyang,jilin

创建表
create table table_array (name string, city array<string>
row format delimited fields terminated by "\t" 

collection items terminated by ",";

数据导入表中

load data local inpath "/home/hadoop/array.txt" into table table_array;


select * from table_array;
select name, city[1] ,city[2],city[3],city[4] from table_array;




 array 使用总结:建表 建表时候用< >  , 查询时候用[ ]

2.2 map 的使用


数据:map.txt
huangbo yuwen:80,shuxue:89,yingyu:95
xuzheng yuwen:70,shuxue:65,yingyu:81
wangbaoqiang yuwen:75,shuxue:100,yingyu:75


create table table_map (name string, score map<string, int>) 
row format delimited fields terminated by "\t" 
collection items terminated by "," 
map keys terminated by ":";


load data local inpath "/home/hadoop/map.txt" into table table_map;


select * from table_map;
select name,score["shuxue"],score["yingyu"],score["yuwen"] from table_map;



map 使用总结:建表 建表时候用< >  , 查询时候用[ ]


2.3 struct 的使用 


数据  struct.txt
1 english,80,true
2 math,89,false
3 chinese,95,true


create table table_struct (id int, info struct<course:string, score:int, sex:boolean>) 
row format delimited fields terminated by "\t" 
collection items terminated by ",";


load data local inpath "/home/hadoop/struct.txt" into table table_struct;


select * from table_struct;

select id, info.course, info.sex from table_struct;


  struct 使用 建表使用 <>  查询时候使用  .访问对应的字段

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值