UDF函式:對字串實現sha256加密,返回64位十六進位制字串
阿新 • • 發佈:2019-02-11
實際需求多一些特殊資料需要加密儲存。下面 實現sha256加密,返回64位十六進位制字串
package cnsuning.udf.functions.string; import org.apache.commons.codec.digest.DigestUtils; import org.apache.hadoop.hive.ql.exec.Description; import org.apache.hadoop.hive.ql.exec.UDF; import org.apache.hadoop.io.Text; /** * 對字串實現sha256加密,返回64位十六進位制字串 */ @Description(name = "sha256" , value = "_FUNC_(string) - get sha256 hash code by given input string." , extended = "Example:\n > select _FUNC_(string) from src;") public class UDFSha256 extends UDF { private Text result = new Text(); public UDFSha256() { } /** * md5 hash. * * @param text * @return */ public Text evaluate(Text text) { if (text == null) { return null; } result.set(DigestUtils.sha256Hex((text.toString()))); return result; } public static void main(String[] args) { UDFSha256 udr = new UDFSha256(); System.out.println(udr.evaluate(new Text("123456"))); } } 測試結果如下 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92