首页 > 代码库 > hive的使用03
hive的使用03
1.hive中的四种排序
1.1 order by :对全局进行排序,只能有一个reduce
select * from hive.employee order by id;
1.2 sort by :对每一个reduce内部数据进行排序,全局结果集没有排序
set mapreduce.job.reduces=3;设置reduce的个数为3
insert overwrite local directory ‘/opt/data/employee_sort_by‘
row format delimited fields terminated by ‘\t‘ collection items terminated by ‘\n‘
select * from hive.employee sort by dept_id;
1.3 distribute by :对数据进行分区,结合sort by进行合并使用,类似于mapreduce中的mapreduce中的partition,必须在sort by 之前
insert overwrite local directory ‘/opt/data/employee_distribute_by‘
row format delimited fields terminated by ‘\t‘ collection items terminated by ‘\n‘
select * from hive.employee distribute by dept_id sort by id asc;
1.4 cluster by:当distribute by 和 sort by 的字段相同时,可以使用cluster by 代替
2.使用udf自定义函数
2.1 编写udf函数
继承extends UDF
编写evaluate 方法
2.2 导入自定义函数到hive函数库
方法一:
add jar /opt/data/jars/my_lower.jar;
create temporary function my_lower as "com.ibeifeng.hive.udf.LowerUdf";
方法二:
create function self_lower as ‘com.ibeifeng.hive.udf.LowerUdf‘ using jar ‘hdfs://life-hadoop.life.com:8020/user/yanglin/data/jars/my_lower.jar‘;
3.hiveserver2的使用
3.1 启动hiveserver2 bin/hiveserver2
3.2 使用beeline进行连接
!connect jdbc:hive2://life-hadoop.life.com:10000 yanglin life@one