hive添加UDF加密函数

目的:实现MD5的加解密

1.写java代码,打jar包:

import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

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

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

import sun.misc.BASE64Encoder;

 

@Description(name = "ToMD5", value = "_FUNC_(str) - Convert str to a MD5(str).")

public class ToMD5 extends UDF{

public String evaluate(String str) throws Exception, Exception {

return EncoderByMd5(str);

}

public String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{

MessageDigest md5=MessageDigest.getInstance("MD5");

BASE64Encoder base64en = new BASE64Encoder();

String newstr=base64en.encode(md5.digest(str.getBytes("utf-8")));

return newstr;

}

}

 

2. 将打好的jar上传到hdfs:

hadoop fs -mkdir /hive_UDF

hadoop fs -put ToMD5.jar /hive_UDF

 

3.进入hive客户端,创建UDF函数。

create function ToMD5 as 'ToMD5' using jar 'hdfs://ocdp/hive_UDF/ToMD5.jar';

 

4.使用创建的UDF函数:

create table test4 as select ToMD5(age) from test1;

 

hive> select * from test4;

OK

wgrU12/pd1mqJ6DJm/9nEA==

xRzkEMEkoQ4NteS5f8KvOQ==

qrMjiSK8wlpvYG61Jf/cVg==

wgrU12/pd1mqJ6DJm/9nEA==

转载于:https://my.oschina.net/u/1755468/blog/3092619

你可能感兴趣的:(hive添加UDF加密函数)