欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hive md5 udf

程序员文章站 2022-07-14 14:39:10
...

java代码:

package testmvn_project;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;

import java.security.MessageDigest;

public class MD5 extends UDF {

    public String evaluate (final String str) {
        if (StringUtils.isBlank(str)){
            return "";
        }
        String digest = null;
        StringBuffer buffer = new StringBuffer();
        try {
            MessageDigest digester = MessageDigest.getInstance("md5");
            byte[] digestArray = digester.digest(str.getBytes("UTF-8"));
            for (int i = 0; i < digestArray.length; i++) {
                buffer.append(String.format("%02x", digestArray[i]));
            }
            digest = buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return digest;
    }
    public static void main (String[] args ) {
        MD5 md5 = new MD5();
        System.out.println(md5.evaluate("12304"));
    }
}

pom.xml信息:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.Ezrealhao</groupId>
  <artifactId>testmvn_project</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>  
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-exec</artifactId>
    <version>1.2.1</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-common</artifactId>
    <version>2.7.3</version>
</dependency>
</dependencies>
</project>

相关标签: hive udf