Ricerca…


Creazione UDF di Hive

Per creare una UDF, dobbiamo estendere la classe UDF ( org.apache.hadoop.hive.ql.exec.UDF ) e implementare il metodo di valutazione.

Una volta che UDF è stato rispettato e il JAR è stato creato, è necessario aggiungere jar al contesto hive per creare una funzione temporanea / permanente.

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 per tagliare la stringa data.

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;
 }
}

esportare il file sopra in jar

Vai alla CLI di Hive e aggiungi l'UDF JAR

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

Verificare che JAR si trovi in ​​Hive CLI Classpath

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

Crea funzione temporanea

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

Uscita UDF

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


Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow