hive自定义函数
pom文件
<dependencies> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-exec</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>3.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
生成0-15随机数java代码
import org.apache.hadoop.hive.ql.exec.UDF; import java.util.Random; public class RandomUDF extends UDF { public int evaluate(String str) { Random random = new Random(); int i = random.nextInt(16); return i; } public static void main(String[] args) { RandomUDF randomUDF=new RandomUDF(); System.out.println(randomUDF.evaluate("fff")); } }
上传hdfs目录
hdfs dfs -put /tmp/hiveudf16.jar hdfs:///tmp/
在impala创建函数或在hue界面
create function if not exists getuuid(STRING) returns int location 'hdfs:///tmp/hiveudf16.jar' symbol='com.sunward.hive.RandomUDF';
删除自定义函数
drop function getuuid(STRING);