您好,登錄后才能下訂單哦!
混合函數可以使用java中的方法java_method(class,method[,arg1[,arg2...]])或者reflect
Hive版本1.2.1
UDTF 用戶定義表函數(表函數)一行變成多行配合lateral view
http://blog.sina.com.cn/s/blog_7e04e0d00101csic.html
UDF 重寫evaluate方法 Map端
import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.io.Text; public class udftest extends UDF{ public boolean evaluate(Text t1,Text t2){ if(t1==null||t2==null){ return false; } double d1=Double.parseDouble(t1.toString()); double d2=Double.parseDouble(t2.toString()); if(d1>d2){ return true; }else{ return false; } } }
函數打包成function.jar
hive命令行
add jar /home/jar/function.jar //jar包進入分布式緩存 create temporary function bigthan as 'com.peixun.udf.udftest'//執行創建模版函數bigthan
UDAF (user defined aggregation function)用戶自定義聚合函數
自定義UDAF統計b字段大于30的記錄個數 countbigthan(b,30)實現代碼
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException; import org.apache.hadoop.hive.ql.metadata.HiveException; import org.apache.hadoop.hive.ql.parse.SemanticException; import org.apache.hadoop.hive.ql.udf.generic.AbstractGenericUDAFResolver; import org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils; import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; import org.apache.hadoop.io.LongWritable; //繼承類型檢查類 public class udaftest extends AbstractGenericUDAFResolver { // 參數個數判斷 @Override public GenericUDAFEvaluator getEvaluator(TypeInfo[] parameters) throws SemanticException { if (parameters.length != 2) { throw new UDFArgumentTypeException(parameters.length - 1, "Exactly two argument is expected"); } return new GenericUDAFCountBigThanEvaluator();// 返回處理邏輯類 } // 處理邏輯類 public static class GenericUDAFCountBigThanEvaluator extends GenericUDAFEvaluator { private LongWritable result; private PrimitiveObjectInspector inputOI1; private PrimitiveObjectInspector inputOI2; // init方法map,reduce階段都得執行 // map階段parameters長度與UDAF輸入的參數個數有關 // reduce階段,parameters長度為1 @Override public ObjectInspector init(Mode m, ObjectInspector[] parameters) throws HiveException { result = new LongWritable(0); inputOI1 = (PrimitiveObjectInspector) parameters[0]; if (parameters.length > 1) { inputOI2 = (PrimitiveObjectInspector) parameters[1]; } return PrimitiveObjectInspectorFactory.writableLongObjectInspector; // 最終結果返回類型 } @Override public AggregationBuffer getNewAggregationBuffer() throws HiveException { CountAgg agg = new CountAgg();// 存放部分聚合值 reset(agg); return agg; } // 緩存對象初始化 @Override public void reset(AggregationBuffer agg) throws HiveException { CountAgg countagg = (CountAgg) agg; countagg.count = 0; } // 具體邏輯 // iterate只在map端運算 @Override public void iterate(AggregationBuffer agg, Object[] parameters) throws HiveException { assert (parameters.length == 2); if (parameters == null || parameters[0] == null || parameters[1] == null) { return; } double base = PrimitiveObjectInspectorUtils.getDouble( parameters[0], inputOI1); double tmp = PrimitiveObjectInspectorUtils.getDouble(parameters[1], inputOI2); if (base > tmp) { ((CountAgg) agg).count++; } } // map階段返回部分結果 @Override public Object terminatePartial(AggregationBuffer agg) throws HiveException { result.set(((CountAgg) agg).count); return result; } // 合并部分結果 map(含有Combiner)和reduce都執行,parial傳遞terminatePartial得到的部分結果 @Override public void merge(AggregationBuffer agg, Object partial) throws HiveException { if (partial != null) { long p = PrimitiveObjectInspectorUtils.getLong(partial, inputOI1); ((CountAgg) agg).count += p; } } @Override public Object terminate(AggregationBuffer agg) throws HiveException { result.set(((CountAgg) agg).count); return result; } public class CountAgg implements AggregationBuffer { long count; } } }
hive注冊永久函數三種方法
hive shell每次啟動都會默認執行$HOME/.hiverc文件
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。