Home > other >  Write the UDF function can wear an array
Write the UDF function can wear an array

Time:09-23

My travels in the hive type is an array of hive

CodePudding user response:

Of course ~ I see you have a written udf. You pass an array & lt; int> How to read the newspaper in the wrong hint is introduced into struct type?

CodePudding user response:

In give you a demo. Used to get data in one of the biggest value
 
Package cn.com.diditaxi.hive.cf;

Import org, apache hadoop. Hive. Ql. Exec. The Description;
Import org, apache hadoop. Hive. Ql. Exec. UDFArgumentException;
Import org, apache hadoop. Hive. Ql. Exec. UDFArgumentTypeException;
Import org, apache hadoop. Hive. Ql. Metadata. HiveException;
Import org, apache hadoop. Hive. Ql. Udf. Generic. GenericUDF;
Import org, apache hadoop. Hive. Ql. Udf. Generic. GenericUDFUtils;
Import org, apache hadoop. Hive. Serde2. Objectinspector. Objectinspector;
Import org, apache hadoop. Hive. Serde2. Objectinspector. ObjectInspectorUtils;
The import org. Apache. Hive. PDK. HivePdkUnitTest;
The import org. Apache. Hive. PDK. HivePdkUnitTests;

/* *
* GenericUDF Class for SQL construct "greatest (value1, value2, value3,... ). "
* Oracle 's CREATEST returns the greatest of the list of one or more expressions.
*
*/
@ the Description (name="greatest" value="HTTP://https://bbs.csdn.net/topics/_FUNC_ (value1, value2, value3,... ) + "
"- Returns the greatest value in the list.",
Extended="Example: \ n" + "& gt; The SELECT _FUNC_ (2, 5, 12, 3) the FROM SRC; 12 "\ n)
@ HivePdkUnitTests (setup="create table dual_data (int I); "
+ "insert the overwrite table dual_data select 1 from dual limit 1;" ,
The cleanup="drop table if the exists dual_data;" ,
Cases in={
@ HivePdkUnitTest (query="SELECT nexr_greatest (2, 5, 12, 3)" +
"FROM dual_data;" , the result="12"),
@ HivePdkUnitTest (query="SELECT nexr_greatest (' 2 ', '5', '12', '3')" +
"FROM dual_data;" , the result="5"),
@ HivePdkUnitTest (query="SELECT nexr_greatest (apples, oranges, 'bannanas)" +
"FROM dual_data;" , the result="oranges")})
Public class GenericUDFGreatest extends GenericUDF {

Private ObjectInspector [] argumentOIs;
Private GenericUDFUtils. ReturnObjectInspectorResolver returnOIResolver;

@ Override
Public ObjectInspector initialize (ObjectInspector [] the arguments) throws UDFArgumentException {

for (int i=0; i If (the arguments [I] getCategory ()!=ObjectInspector. Category. PRIMITIVE) {
Throw new UDFArgumentTypeException (I, "Only primitive type, the arguments are accepted but"
+ the arguments [I] getTypeName () + "is passed.");
}
}

ArgumentOIs=the arguments;
ReturnOIResolver=new GenericUDFUtils. ReturnObjectInspectorResolver (true);
for (int i=0; i if (! ReturnOIResolver. Update (the arguments [I]) {
Throw new UDFArgumentTypeException (I, "The value of The return should have The same type: " "
+ returnOIResolver. The get (). GetTypeName () +
"" is expected but \ "" + the arguments [I] getTypeName ()
+ "" is found ");
}
}

Return returnOIResolver. The get ();
}

@ Override
Public Object the evaluate (DeferredObject [] fields) throws HiveException {
The Object greatestObject=null;
ObjectInspector greatestOI=null;

for (int i=0; i Object fieldObject=fields [I] the get ();
If (greatestObject==null) {
GreatestObject=fieldObject;
GreatestOI=argumentOIs [I];
continue;
}

If (ObjectInspectorUtils.com pare said (greatestObject greatestOI, fieldObject, argumentOIs [I]) & lt;=0) {
GreatestObject=fieldObject;
GreatestOI=argumentOIs [I];
}
}

Return returnOIResolver. ConvertIfNecessary (greatestObject greatestOI);
}

@ Override
Public String getDisplayString (String [] children) {
StringBuilder sb=new StringBuilder();
Sb. Append (greatest (" ");
for (int i=0; i Sb. Append (children [I]), append (", ");
}
Sb. Append (children [children] length - 1), append (") ");
return sb.toString();
}

}

  • Related