使用的是udf示例代码。
编译sh:
#!/bin/sh
if [ $# -ne 1 ]; then
echo "Usage: $0 <java file="">"
exit 1
fi
CNAME=${1%.java}
JARNAME=$CNAME.jar
JARDIR=$CNAMECLASSPATH=$(ls
$HIVE_HOME/lib/hive-serde-*.jar):$(ls $HIVE_HOME/lib/hive-exec-*.jar):$(ls $HADOOP_HOME/hadoop-
core-*.jar)
echo "javac -classpath $CLASSPATH -d $JARDIR/ $1 && jar -cf
$JARNAME -C $JARDIR/ . && tell $1"
tell() {
echo
echo "$1 successfully compiled."
echo
}
mkdir -p $JARDIR
javac -classpath $CLASSPATH -d $JARDIR/ $1 && jar -cf $JARNAME
-C $JARDIR/ . && tell $1
代码:
package com.example.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
public final class Lower extends UDF {
public Text evaluate(final Text s) {
if (s == null) {
return null;
}
return new Text (s.toString().toLowerCase());
}
}
编译:
> sh compile.sh Lower.java
Lower.java successfully compiled.
> ls Lower*
Lower.jar Lower.java Lower
部署:
hive>add jar Lower.jar;hive>create temporary function my_lower as 'com.example.hive.udf.Lower';
测试:
select my_lower from xxtable;
--转自