本博文的主要内容如下:
.hive的详细官方手册
.hive支持的数据类型
.Hive Shell
.Hive工程所需依赖的jar包
.hive自定义函数
.分桶4
.附PPT
hive的详细官方手册
标准的SQL,hive都支持。
这就是,为什么目前hive占有市场这么多,因为,太丰富了,当然,Spark那边的Spark SQL,也在不断地进步。
非常多,自行去研究,这里不多赘述。
将查询结果,写到本地文件或hdfs里的文件
//write to hdfs
insert overwrite local directory '/home/hadoop/hivetemp/test.txt' select * from tab_ip_part where part_flag='part1'; //路径可以是Linux本地的
insert overwrite directory '/hiveout.txt' select * from tab_ip_part where part_flag='part1'; //路径也可以是hdfs里的
这里,不演示
//array
create table tab_array(a array,b array)
row format delimited
fields terminated by '\t'
collection items terminated by ',';
示例数据
tobenbrone, laihama,woshishui 13866987898,13287654321
abc,iloveyou,itcast 13866987898,13287654321
select a[0] from tab_array;
select * from tab_array where array_contains(b,'word');
insert into table tab_array select array(0),array(name,ip) from tab_ext t;
//map
create table tab_map(name string,info map)
row format delimited
fields terminated by '\t'
collection items terminated by ';'
map keys terminated by ':';
示例数据:
fengjie age:18;size:36A;addr:usa
furong age:28;size:39C;addr:beijing;weight:180KG
load data local inpath '/home/hadoop/hivetemp/tab_map.txt' overwrite into table tab_map;
insert into table tab_map select name,map('name',name,'ip',ip) from tab_ext;
这里,不多赘述。
//struct
create table tab_struct(name string,info struct)
row format delimited
fields terminated by '\t'
collection items terminated by ','
load data local inpath '/home/hadoop/hivetemp/tab_st.txt' overwrite into table tab_struct;
insert into table tab_struct select name,named_struct('age',id,'tel',name,'addr',country) from tab_ext;
这里,不多赘述。
Hive Shell
//cli shell
hive -S -e 'select country,count(*) from tab_ext' > /home/hadoop/hivetemp/e.txt
有了这种执行机制,就使得我们可以利用脚本语言(bash shell,python)进行hql语句的批量执行
select * from tab_ext sort by id desc limit 5;
select a.ip,b.book from tab_ext a join tab_ip_book b on(a.name=b.name);
思考一个问题:就说,一个业务场景里面,写sql语句去分析作统计,往往不是一句sql语句能搞定的,sql对一些字段或函数或自定义函数处理会得出一些中间结果,中间结果存在中间表里,然后,才可进入下一步的处理。可能,你需写好多条sql语句,按照批量,流程去走,以前在关系型数据库里,是按照流程处理过程做的。
hive里,不支持存储过程的语法,那若有一个模型,这个模型里有十几个sql语句,一条一条写很麻烦,那么,hive在想,能不能组织成批量去运行呢?则借外部的工作(如写一个shell脚本,执行十几个sql语句)。
可以参照
在shell下,接收。
//cli shell
hive -S -e 'select country,count(*) from tab_ext' > /home/hadoop/hivetemp/e.txt
有了这种执行机制,就使得我们可以利用脚本语言(bash shell,python)进行hql语句的批量执行
select * from tab_ext sort by id desc limit 5;
select a.ip,b.book from tab_ex