hive udf 例子

import org.apache.hadoop.hive.ql.exec.UDF;apache


// 假如報異常,則體如今hive中的值是:NULLoop

public class HiveUdf  extends UDF{lua


public String evaluate(String str) {spa

 

new RuntimeException("hive udf is Error");hadoop

return null;it

}io

}function


/**class

 * hive udf 求 top k import

 * add jar /opt/smc/xuanli/data/rank.jar;

create temporary function rank as 'com.hive.udf.Rank';

select * from (select a.name,rank(a.name) as rank,a.age from (select t1.name,t1.age from t1 distribute by name sort by name,age desc)a)b where b.rank<2;



 */

import org.apache.hadoop.hive.ql.exec.UDF;

public class Rank extends UDF{

int num=0;

String key=null;

 

public  int evaluate( final  String str) {

 

if(!str.equals(key)){

 

key = str;

num=0;

}

 

return num++;

 

}

}

相關文章
相關標籤/搜索