1. 程式人生 > 其它 >hive自定義函式

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/hiveudf.jar hdfs:///tmp/udf/

在impala建立函式或在hue介面

create function if not exists getrandom(STRING) returns int location 'hdfs:///tmp/hiveudf.jar' symbol='com.hive.RandomUDF';