hive udf 例子

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


// 假如报异常,则体现在hive中的值是:NULL

public class HiveUdf  extends UDF{


public String evaluate(String str) {

 

new RuntimeException("hive udf is Error");

return null;

}

}

/**

 * hive udf 求 top k 

 * 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++;

 

}

}


你可能感兴趣的:(hive udf 例子)