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;
}
}
export the above to jar file
Go to the Hive CLI and Add the UDF JAR
hive> ADD jar /home/cloudera/Hive/hive_udf_trim.jar;
Verify JAR is in Hive CLI Classpath
hive> list jars;
/home/cloudera/Hive/hive_udf_trim.jar
Create Temporary Function
hive> CREATE TEMPORARY FUNCTION STRIP AS 'MyHiveUDFs.Strip';
UDF Output
hive> select strip(' hiveUDF ') from dummy;
OK
hiveUDF