hive java 编程_hql语法及自定义函数(含array、map讲解) + hive的java api

本博文的主要内容如下:

.hive的详细官方手册

.hive支持的数据类型

.Hive Shell

.Hive工程所需依赖的jar包

.hive自定义函数

.分桶4

.附PPT

hive的详细官方手册

791c2d71dfa72d7ada74c359dfbf4117.png

beb3f23036a1eb32dc5312d838b48513.png

7a034361037eddd1263edc3654cf3fa3.png

标准的SQL,hive都支持。

这就是,为什么目前hive占有市场这么多,因为,太丰富了,当然,Spark那边的Spark SQL,也在不断地进步。

75eba8fb6ed1ba6cdbbf5ec8c4f664c3.png

f09cf2a74e39aa0d180ff5e73083820e.png

非常多,自行去研究,这里不多赘述。

将查询结果,写到本地文件或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;

e61444aa4467870c1fad8e1f0b452c6f.png

137cd34b31683a15ce76a1e8d4f2d139.png

//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语句)。

可以参照

4b04626dcb72dd8a4519e8108f149135.png

在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值