为hive的每条数据添加row_number, 首先添加行号,必须考虑到数据必须放在一个reduce中去执行。
为hive的每条数据添加row_number, 首先添加行号,必须考虑到数据必须放在一个reduce中去执行。先上代码
package xx.xxxxx.hive.udf;
import org.apache.Hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.udf.UDFType;
@UDFType(deterministic = false)
public class RowNumber extends UDF {
private static int MAX_VALUE = 50;
private static String comparedColumn[] = new String[MAX_VALUE];
private static int rowNum = 1;
public int evaluate(Object ...args) {
String columnValue[] = new String[args.length];
for (int i = 0; i columnValue[i] = args[i].toString();
}
if (rowNum == 1) {
for (int i = 0; i comparedColumn[i] = columnValue[i];
}
for (int i = 0; i if (!comparedColumn[i].equals(columnValue[i])) {
for (int j = 0; j comparedColumn[j] = columnValue[j];
}
rowNum = 1;
return rowNum++;
}
}
return rowNum++;
}
}
打包jar包,并创建函数。
add jar /home/hdbatch/jars/iclickhiveudf.jar;
create temporary function row_number as 'cn.iclick.hive.udf.RowNumber';
但是用法要注意,假设我要对一个表的数据进行标注行号,,两条sql语句,
create table test_tony as select row_number(1), tid from(select distinct tid from cookie where i_date=20131105)t order by tid;
上边这条语句会标注行号错误,会产生11个reduce,所以会打11份相同的row number,所以就会有错误,为什么会有不同的解释呢??? 看explain sql语句, 原因是编写non-deterministic的UDF时遇到的谓词下推错误。
具体详见:
免费 盛世企业网站管理系统(SnSee)系统完全免费使用,无任何功能模块使用限制,在使用过程中如遇到相关问题可以去官方论坛参与讨论。开源 系统Web代码完全开源,在您使用过程中可以根据自已实际情况加以调整或修改,完全可以满足您的需求。强大且灵活 独创的多语言功能,可以直接在后台自由设定语言版本,其语言版本不限数量,可根据自已需要进行任意设置;系统各模块可在后台自由设置及开启;强大且适用的后台管理支
0
更多详情见请继续阅读下一页的精彩内容:
Hive 的详细介绍:请点这里
Hive 的下载地址:请点这里
相关阅读:
基于Hadoop集群的Hive安装
Hive内表和外表的区别
Hadoop + Hive + Map +reduce 集群安装部署
Hive本地独立模式安装
Hive学习之WordCount单词统计

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号