环境:

  • idea 2017
  • maven 4.0
  • hive 1.2.1.2.6
  • win10

创建maven项目

 1
 2<?xml version="1.0" encoding="UTF-8"?>
 3<project xmlns="http://maven.apache.org/POM/4.0.0"
 4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 6    <modelVersion>4.0.0</modelVersion>
 7
 8    <groupId>hive</groupId>
 9    <artifactId>udf</artifactId>
10    <version>1.0-SNAPSHOT</version>
11    <packaging>jar</packaging>
12
13    <name>hive</name>
14    <url>http://maven.apache.org</url>
15
16    <properties>
17        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18    </properties>
19
20    <dependencies>
21        <dependency>
22            <groupId>org.apache.hive</groupId>
23            <artifactId>hive-exec</artifactId>
24            <version>0.13.0</version>
25        </dependency>
26    </dependencies>
27
28</project>

等待安装好依赖

编写UDF函数

编写一个生成MD5函数

 1public class my_udf extends UDF {
 2
 3    public String my_udf(String s) {
 4        return getMD5(s);
 5    }
 6    public static String getMD5(String str) {
 7        try {
 8            // 生成一个MD5加密计算摘要
 9            MessageDigest md = MessageDigest.getInstance("MD5");
10            // 计算md5函数
11            md.update(str.getBytes());
12            // digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
13            // BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
14            return new BigInteger(1, md.digest()).toString(16);
15        } catch (Exception e) {
16            System.out.print("MD5加密出现错误");
17        }
18        return "error";
19    }
20
21
22}

生成jar包

idea 里面直接点package

上传jar包

使用jar文件

1add jar hdfs:///user/admin/udf-md5.jar;
2create temporary function my_udf as 'my_udf';
3select my_udf(word_count.column1) from word_count limit 5