为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时遇到的谓词下推错误。
具体详见:
更多详情见请继续阅读下一页的精彩内容:
hive 的详细介绍:请点这里
hive 的下载地址:请点这里
相关阅读:
基于hadoop集群的hive安装
hive内表和外表的区别
hadoop + hive + map +reduce 集群安装部署
hive本地独立模式安装
hive学习之wordcount单词统计
