Sök…


Hive UDF skapande

För att skapa en UDF måste vi utöka UDF-klassen ( org.apache.hadoop.hive.ql.exec.UDF ) och implementera utvärderingsmetoden.

När UDF har följts och JAR har byggts, måste vi lägga till burk till hive-sammanhang för att skapa en tillfällig / permanent funktion.

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

class UDFExample extends UDF {
  
  public String evaluate(String input) {
    
    return new String("Hello " + input);
  }
}

hive> ADD JAR <JAR NAME>.jar;
hive> CREATE TEMPORARY FUNCTION helloworld as 'package.name.UDFExample';
hive> select helloworld(name) from test;

Hive UDF för att trimma den givna strängen.

package MyHiveUDFs;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

public class Strip extends UDF {

private Text result = new Text();
 public Text evaluate(Text str) {
 if(str == null) {
 return null;
 }
 result.set(StringUtils.strip(str.toString()));
 return result;
 }
}

exportera ovanstående till jarfil

Gå till Hive CLI och lägg till UDF JAR

hive> ADD jar /home/cloudera/Hive/hive_udf_trim.jar;

Verifiera att JAR finns i Hive CLI Classpath

hive> list jars;
/home/cloudera/Hive/hive_udf_trim.jar

Skapa tillfällig funktion

hive> CREATE TEMPORARY FUNCTION STRIP AS 'MyHiveUDFs.Strip';

UDF-utgång

 hive> select strip('   hiveUDF ') from dummy;
 OK
 hiveUDF


Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow