hive练习(一)

1.创建article表(建内部表)

create table article(sentence string)
row format delimited fields terminated by '\n'
--从本地导入数据
load data local inpath '/home/wl/mapreduce_wordcount_python/The_Man_of_Property.txt' overwrite into table article;

2.在hive中wordcount

第一步 :按照空格把每个单词分割
①.select explode(split(sentence, ' ')) as word from article limit 10;
第二步:对上一步的word做聚合
select word ,count(*)  from (select explode(split(sentence,' ' )) as word from article limlit 10) t group by word limit 10;

Hive架构流程图
在这里插入图片描述
3.hive表的本质就是hadoop的hdfs的文件
何时做分区表?
例如实时的数据:200亿条用户的行为数据,从10000亿中 查询200亿 ,每天都生成数据
。每天的数据都会放到对应的文件夹里面:
dt=20180414
我们只需要到特定的文件夹中查找
select * from badou.news where dt=‘20180414’
where dt in(‘20180414’,‘20180413’),如果这里面有20多天的数据,则用脚本写,然后把脚本copy到里面执行。
还有我们和别的表join时,他的表中dt如果不做分区表,会特别大的数据,很复杂执行效率低

4.set hive.cli.print.header=true;
设置这个我们在查表时就显示列名称了
select * from udata limit 10;
创建udata表

 CREATE TABLE udata (     
   user_id INT,  
   item_id INT,  
   rating INT,    
   timestamps STRING)  
   ROW FORMAT DELIMITED  
   FIELDS TERMINATED BY '\t'  
   STORED AS TEXTFILE;
	
加载数据:
LOAD DATA LOCAL INPATH '/home/wl/hive/ml-100k/u.data'  OVERWRITE INTO TABLE udata; 

做分桶:
196 242 3 881250949
186 302 3 891717742
22 377 1 878887116
244 51 2 880606923
user_id 分3个桶:(取模,最后只能取0,1,2)
196%3=1
186%3 = 0
22%3 = 1
23%3=2

设置4个buckets

--相当于设定了4个reduce,就会有四个文件生成

首先设置这个
set hive.enforce.bucketing = true;
创建
create table bucket_user (id int)clustered by (id) into 4 buckets;
插入数据:     cast()是转换类型
insert overwrite table bucket_user select cast(userid as int) from badou.udata;
数据的抽样查询:1/16 * 4 = 1/4.    1/4的数据量
select * from badou.bucket_user tablesample(bucket 1 out of 16 on id) limit 10;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值