hive中使用自定义函数(UDF)实现分析函数row_number的功能
来源:互联网 发布:caffe python layer 编辑:程序博客网 时间:2024/05/17 06:19
hive> desc row_number_test;
OK
id1
id2
age
score
name
hive> select * from row_number_test;
OK
2
1
1
2
2
1
1
使用时要先在子查询中进行分区与排序,比如oracle中这样一句SQL:
select row_number() over (partition by id1 order by age desc)from row_number_test;
转换为hive语句应该是:
select row_number(id1) from
如果partition by 两个字段:
select row_number() over (partition by id1,id2 orderby
转换为hive语句应该是:
select row_number(id1,id2)
展示一下查询结果:
1.
select id1,id2,age,score,name,row_number(id1) rn from (select *from row_number_test distribute by id1 sort by id1,age desc) a;
OK
2
2
2
1
1
1
1
2.
select id1,id2,age,score,name,row_number(id1,id2) rn from(select * from row_number_test distribute by id1,id2 sortby id1,id2,score) a;
OK
2
1
2
1
1
1
2
下面是代码,只实现了接收1个参数和2个参数的evaluator方法,参数再多的照搬代码就可以了,代码仅供参考:
package org.rowincrement;import java.util.ArrayList;import org.apache.hadoop.hive.ql.exec.UDFArgumentException;import org.apache.hadoop.hive.ql.metadata.HiveException;import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;public class RowIncrement extends GenericUDTF { Object[] result = new Object[1]; @Override public void close() throws HiveException { } @Override public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException { if (args.length != 1) { throw new UDFArgumentLengthException("RowIncrement takes only one argument"); } if (!args[0].getTypeName().equals("int")) { throw new UDFArgumentException("RowIncrement only takes an integer as a parameter"); } ArrayList<String> fieldNames = new ArrayList<String>(); ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>(); fieldNames.add("col1"); fieldOIs.add(PrimitiveObjectInspectorFactory.javaIntObjectInspector); return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames,fieldOIs); } @Override public void process(Object[] args) throws HiveException { try { int n = Integer.parseInt(args[0].toString()); for (int i=0;i<n;i++) { result[0] = i+1; forward(result); } } catch (Exception e) { throw new HiveException("RowIncrement has an exception"); } } public static void main(String args[]) { Row_number t = new Row_number(); System.out.println(t.evaluate(123)); System.out.println(t.evaluate(123)); System.out.println(t.evaluate(123)); System.out.println(t.evaluate(1234)); System.out.println(t.evaluate(1234)); System.out.println(t.evaluate(1234)); System.out.println(t.evaluate(1235)); } }
- hive中使用自定义函数(UDF)实现分析函数row_number的功能
- hive中使用自定义函数(UDF)实现分析函数row_number的功能
- Hive UDF实现分析函数row_number
- Hive UDF实现分析函数row_number
- Hive 中自定义UDF函数
- 编写hive udf和使用hive udf:hue的hive界面中使用hive udf函数、oozie使用hive udf函数、hive命令行使用udf函数
- 编写hive udf和使用hive udf:hue的hive界面中使用hive udf函数、oozie使用hive udf函数、hive命令行使用udf函数
- HIVE 自定义函数 UDF
- Hive自定义函数-UDF
- hive自定义函数UDF
- Hive自定义函数UDF
- Hive自定义UDF函数
- 自定义HIVE-UDF函数
- hive自定义函数UDF
- Hive中实现自定义函数UDF及打包
- Hive中实现自定义函数UDF详细过程
- hive的UDF实现类似于oracle的decode函数功能
- hive中如何自定义编写UDF函数
- OpenCV之灰度直方图反向投影(图像相似性检测)
- BOM-读书笔记二
- Ubuntu 计划任务详解
- thinkphp中模版引擎中常用的变量输出
- 搭建strus2 + spring + hibernate (三)spring管理hibernate SessionFactory 篇
- hive中使用自定义函数(UDF)实现分析函数row_number的功能
- Ubuntu中SSH命令详解
- 多态
- HD problem
- 微薄推广测试
- 运维故障
- VxWorks驱动程序开发指南(四)--驱动程序的组织结构
- Proxy SwitchySharp地址
- spring管理hibernate数据库连接(1)