impala 建立udf MD5函數

package com.netease.hive.udf;java

import java.security.MessageDigest;shell

import org.apache.hadoop.hive.ql.exec.UDF;apache

public class MD5UDF extends UDF {app

    public static String evaluate(String value) {
        StringBuilder sb = new StringBuilder();
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("md5");
            byte[] bytes = messageDigest.digest(value.getBytes());
            for (int i = 0; i < bytes.length; i++) {
                int tempInt = bytes[i] & 0xff;
                if (tempInt < 16) {
                    sb.append(0);
                }
                sb.append(Integer.toHexString(tempInt));
            }
        } catch (Exception e) {oop

        }
        return sb.toString();
    }ui

    public static void main(String[] args) {
        String hello = "hello world";
        System.out.println("MD5加密後的結果:" + evaluate(hello));
    }加密

}
 lua

1.須要導入hive-exec-*.jar;.net

2.extends UDF,繼承接口UDF;繼承

3.實現方法evaluate();

hadoop fs -put ./md5UDf.jar hdfs://nameservice1/user/hive
impala-shell -q "create function md5(string) returns string location 'hdfs://nameservice1/user/hive/md5UDf.jar' symbol='com.netease.hive.udf.MD5UDF';"

驗證一下:

impala-shell -q "select md5('tanj')";

相關文章
相關標籤/搜索