UDF 编写与注册

功能

对数据加解密方式处理

环境

hadoop 2.6.0+cdh5.11.2

hive 1.1.0+cdh5.11.2

java 1.8.xxx

配置

新加入hadoop集群的服务器需要放/opt/logs/env.properties, 配置内容从其它已存在的服务器上copy

pom.xml

<dependency>

 <groupId>com.bailian.cryptgroupId>

 <artifactId>crypt-coreartifactId>

 <version>0.0.1.1version>

 <exclusions>

 <exclusion>

 <artifactId>httpclientartifactId>

 <groupId>org.apache.httpcomponentsgroupId>

 exclusion>

 <exclusion>

 <artifactId>guavaartifactId>

 <groupId>com.google.guavagroupId>

 exclusion>

 <exclusion>

 <artifactId>gsonartifactId>

 <groupId>com.google.code.gsongroupId>

 exclusion>

 exclusions>

dependency>

 
  

注册Hive UDF

public class XXDeCode extends UDF {

 public String evaluate(final String enCodeString) {

 if (enCodeString == null) {

 return null;

 }

 String deCodeStr = EncryptUtil.decrypt(enCodeString);

 return deCodeStr;

 }

}

public class XXEnCode extends UDF {

 public String evaluate(final String rawString) {

 if (rawString == null) {

 return null;

 }

 String enCode = EncryptUtil.encrypt(rawString);

 return enCode;

 }

}

public class XXBDEnCode extends UDF {

 public String evaluate(final String rawString) {

 if (rawString == null) {

 return null;

 }

 return Md5Util.md5(rawString);

 }

}

应用

-- 解密

CREATE FUNCTION bl_decode as 'com.bl.bigdata.tool.hiveextend.BlDeCode' USING JAR 'hdfs:///xxxx.jar';

-- 加密

CREATE FUNCTION bl_encode as 'com.bl.bigdata.tool.hiveextend.BlEnCode' USING JAR 'hdfs:///xxx.jar';

-- 加密

CREATE FUNCTION bl_bd_encode as 'com.bl.bigdata.tool.hiveextend.BlBDEnCode' USING JAR 'hdfs:///xxxx.jar';

注意:

1 hive beeline连接注册UDF函数时,有多个server,需要联几次做重复操作

2 spark2.1.3-211版本存在bug不能直接用注册的UDF. 建议用spark2.4.5-211

你可能感兴趣的:(bigdata,hive,spark)