cdh6.3.2的hive配udf

文章讲述了大数据平台用户在使用自定义UDF时遇到的问题,即如何在多个HS2环境中共享UDF,需将文件上传至HDFS并手动在每个HS2上创建函数,且Sentry权限不影响。同时提到了Maven项目配置和Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

大数据平台的租户要使用udf,他们用beeline连接,
意味着要通过hs2,但如果有多个hs2,各个hs2之间不能共享,需要先把文件传到hdfs,然后手动在各hs2上create function。之后就可以永久使用了,重启hs2也可以

调研

先查的hive官网

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-CreatingCustomUDFs
在这里插入图片描述
用beeline执行add jar 和create function,但发现只在当前的hs2生效

然后查cdh官网

cdh的官网上说配UDF,需要考虑是否重启hs2,是否启用sentry,列出了3种方案。
https://docs.cloudera.com/documentation/enterprise/latest/topics/cm_mc_hive_udf.html
在这里插入图片描述
Direct JAR reference configuration
Straight-forward, but recommended for development only. Does not support Sentry.
试了下,是永久的,重启仍然生效,但只对当前的hs2有效,如果有多个hs2,需要在每个hs2上都执行create function命令
虽然我们开了sentry,但没影响,sentry仍然有效

pom

<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.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>sm3UDF</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>sm3UDF</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.68</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.1.1</version>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>junit</groupId>-->
<!--            <artifactId>junit</artifactId>-->
<!--            <version>4.13.2</version>-->
<!--            <scope>test</scope>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>2.1.1-cdh6.3.2</version>
        </dependency>

    </dependencies>

    <build>
    <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
    </plugins>
    </build>
</project>

java

package org.picc.encrypt;

import org.apache.commons.codec.binary.Hex;
import org.apache.hadoop.io.Text;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.apache.hadoop.hive.ql.exec.UDF;

public class Sm3Fun extends UDF{

	 public static String sm3(String saltBefore, String text, String saltAfter) {

        if (text == null) {
            return null;
        }

        Text result = new Text();
        SM3Digest digest = new SM3Digest();

        Text sb = new Text(saltBefore);
        Text value = new Text(text);
        Text sa = new Text(saltAfter);

        byte[] hashData = new byte[32];

        digest.reset();

        digest.update(sb.getBytes(), 0, sb.getLength());
        digest.update(value.getBytes(), 0, value.getLength());
        digest.update(sa.getBytes(), 0, sa.getLength());
        digest.doFinal(hashData, 0);
        String sm3Hex = Hex.encodeHexString(hashData);

        result.set(sm3Hex);
        return result.toString();
    }

    public String evaluate(String text) {

        if (text == null) {
            return null;
        }

        Text result = new Text();
        SM3Digest digest = new SM3Digest();

        Text value = new Text(text);


        byte[] hashData = new byte[32];

        digest.reset();

        digest.update(value.getBytes(), 0, value.getLength());
        digest.doFinal(hashData, 0);
        String sm3Hex = Hex.encodeHexString(hashData);

        result.set(sm3Hex);
        return result.toString();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值