hive相关实践操作

hive建表语句:

1.建内部表

hive (badou)> create table udata(userid int,item_id int,rating int,`timestamp` timestamp)row format delimited fields terminated by ' ';
OK
Time taken: 2.254 seconds
hive (badou)> show tables;
OK
udata

create table  if not exists inner_test (
aisle_id string,                                      
aisle_name string     
)
row format delimited fields terminated by ',' lines terminated by '\n'  
stored as textfile  
location '/data/inner';


2.建外部表

create external table  if not exists ext_test (
aisle_id string,                                      
aisle_name string     
)
row format delimited fields terminated by ',' lines terminated by '\n'  
stored as textfile  
location '/data/ext';

3.建分区表

create table partition_test(
order_id string,                                      
user_id string,                                      
eval_set string,                                      
order_number string,  
order_hour_of_day string,                                      
days_since_prior_order string
)partitioned by(order_dow string)
row format delimited fields terminated by '\t';

hive导入数据:

1.从文件导入数据到表

hive (badou)> load data local inpath '/home/dongdong/hive/u.data' overwrite into table udata;
Loading data to table badou.udata
OK
Time taken: 2.335 seconds

2.给分区表插入数据

insert overwrite table partition_test partition (order_dow='1')
select order_id,user_id,eval_set,order_number,order_hour_of_day,days_since_prior_order from orders where order_dow='1' limit 10;

Hive删除表中数据

方式一:仅删除表中数据,保留表结构

truncate table 表名;

(truncate用于删除所有的行,这个行为在hive元存储删除数据是不可逆的)
或  delete from 表名 where 1 = 1 ;   

(delete用于删除特定条件下的行,使用where 1=1 删除所有行  SQL中where 1 = 1 的使用)

truncate 不能删除外部表!因为外部表里的数据并不是存放在Hive Meta store中

方式二:删除整个表

drop table 表名;
如果要永久性删除,不准备再恢复:
drop table 表名 purge;

不用终端的方式执行sql

hive -f create_table.sql

hive实现wordcount的sql语句

select word,count(*)
from (
select 
explode(split(sentence,''))
as word
from article
)t
group by word

hive的udf,udtf内置函数

udf实践:

1.java代码并打包成jar:

2.将jar添加到hive中,并创建临时函数:

3.使用函数及结果:

udtf实践:

1.编写java代码并打包成jar:

2.添加到hive,并创建临时函数:

3.载入数据并创建hive表:

4.使用函数及结果:

transform(可用shell语言,python语言开发)

shell语言开发:

1.编写awk文件:

2.加入hive

3.使用awk脚本,及结果:

python实践:

使用1:

把py文件添加到hive中:

使用及结果:

wordcount实践:

1.创建hive表

2.导入数据:

创建另一张表(存储):

新建map.py,red.py并add到hive:

使用及结果:

把wordcount结果插入到指定表中:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曾牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值