Hive自定义函数支持国密SM4解密

当前项目背景需要使用到国密SM4对加密后的数据进行解密,Hive是不支持的,尝试了华为DWS数仓,华为只支持在DWS中的SM4加密解密,不支持外部加密数据DWS解密

新建Maven工程

只需要将引用的第三方依赖打到jar包中,hadoop和hive的依赖不需要打,不需要打的依赖scope选择provided即可。
使用idea新建maven工程,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.0modelVersion>

    <groupId>com.szc.bigdata.hivegroupId>
    <artifactId>sm4_decodeartifactId>
    <version>0.1version>
    <properties>
        <hadoop.version>3.1.1-hw-ei-311006hadoop.version>
        <hive.version>3.1.0-hw-ei-311006hive.version>
    properties>

    <dependencies>
      
        <dependency>
            <groupId>commons-codecgroupId>
            <artifactId>commons-codecartifactId>
            <version>1.15version>
        dependency>
        <dependency>
            <groupId>org.bouncycastlegroupId>
            <artifactId>bcprov-jdk15to18artifactId>
            <version>1.69version>
        dependency>
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-cryptoartifactId>
            <version>5.8.16version>
        dependency>
        <dependency>
            <groupId>org.apache.hadoopgroupId>
            <artifactId>hadoop-authartifactId>
            <version>${hadoop.version}version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.apache.hivegroupId>
            <artifactId>hive-jdbcartifactId>
            <version>${hive.version}version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.apache.hivegroupId>
            <artifactId>hive-commonartifactId>
            <version>${hive.version}version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.apache.hivegroupId>
            <artifactId>hive-shimsartifactId>
            <version>${hive.version}version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.apache.hadoopgroupId>
            <artifactId>hadoop-commonartifactId>
            <version>${hadoop.version}version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.apache.hivegroupId>
            <artifactId>hive-execartifactId>
            <version>${hive.version}version>
            <scope>providedscope>
        dependency>

    dependencies>
    <repositories>
      
        <repository>
            <id>huaweicloudsdkid>
            <url>https://mirrors.huaweicloud.com/repository/maven/huaweicloudsdk/url>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
        repository>
    repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-assembly-pluginartifactId>
                <version>3.1.0version>
                <configuration>
                  
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependenciesdescriptorRef>
                    descriptorRefs>
                    <archive>
                        <manifest>
                            <addClasspath>trueaddClasspath>
                            <mainClass>com.szc.bigdata.hive.udf.SM4DecodemainClass>
                        manifest>
                    archive>
                configuration>
                <executions>
                    <execution>
                        <id>make-assemblyid>
                        
                        <phase>packagephase>
                        <goals>
                            <goal>singlegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

编写自定义函数类

SmUtil引用的是hutools里的工具类

public class SM4Decode extends UDF {
    public String evaluate(String data, String key) {
        if (data == null || "".equals(data)) {
            return null;
        }
        SymmetricCrypto sm4 = SmUtil.sm4(key.getBytes());
        return  StringUtils.newStringUtf8(sm4.decrypt(data));
    }
}

上传jar包到hdfs上

上传jar包到hdfs上

如果集群开启了权限控制,kerberos需要先试用kinit登录才可以

# 刷新环境变量
source bigdate_env

# kinit 登录
kinit <用户名>
# 回车后输入密码

# 上传到指定目录
hdfs dfs -put ~/sm4_decode-3.1.0-hw-ei-311006-jar-with-dependencies.jar /tmp

# 授权
hdfs dfs -chmod 777 /tmp/sm4_decode-3.1.0-hw-ei-311006-jar-with-dependencies.jar

创建函数

# 进入到hive目录下执行beeline
beeline

# 授权admin权限
set role admin;

# 创建函数
CREATE   FUNCTION sm4decode AS 'com.szc.bigdata.hive.udf.SM4Decode' 
	using jar 'hdfs:///tmp/sm4_decode-3.1.0-hw-ei-311006-jar-with-dependencies.jar';
 
# 创建临时函数
CREATE TEMPORARY  FUNCTION sm4decode AS 'com.szc.bigdata.hive.udf.SM4Decode' 
	using jar 'hdfs:///tmp/sm4_decode-3.1.0-hw-ei-311006-jar-with-dependencies.jar';

# 使用函数
select sm4decode('decodestr','key');

你可能感兴趣的:(Hive,Java,hive,hadoop,数据仓库)