目的:设置了 3 个reduce,在最终生成的 3 个文件中,实现全局是按year升序,year相同则按温度降序排列
启动hive
1、启动服务端,注意:删除掉hadoop脚本中的调试设置代码
hiveserver2
2、启动客户端
beeline
!connect jdbc:hive2://localhost:10000/xxxxxx;auth=noSasl; //装hue后
操作hive
建表
create table tmp(id int,t int)row format delimited fields terminated by ' ';
alter table tmp change id year int; //修改字段名
加载数据
load data local inpath 'ttt.dat' into table tmp;
注:local本地文件:相当于复制;不加local,从hdfs上加载数据,相当于移动
设置reduce个数
set mapreduce.job.reduces = 3;
使用ctas语法测试
create table tmp2 as select * from tmp distribute by (case when year < 1930 then 0 when year > 1960 then 2 else 1 end) sort by (year asc ,t desc);
知识点:1、ctas:有结构有数据,但分区变成普通字段
ctal:有结构没数据
2、distribute by hash分区,和reduce个数取模,该例中有 3 个reduce,最终三个文件
order by hive中实现全排序方式,只有一个reduce,因此数据量很大时,性能不好;因此使用时,一般加 limit
sort by 部分排序
cluster by distribute by + sort by
3、case when then end语法