hive-udf-不重复uuid

适用模块

hiveudf

具体说明

中台HiveServer2为了安全,禁用了部分udf函数,可以通过set命令查看
regexp_replace(reflect("java.util.UUID", "randomUUID"), "-", "")

set hive.server2.builtin.udf.blacklist;
--结果为
 hive.server2.builtin.udf.blacklist=reflect,reflect2,java_method

hiveudf  uuid不重复

使用示例

##### 依赖配置
对应pom.xml文件依赖:

<dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--添加hive依赖-->
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>2.1.1</version>
        </dependency>

    </dependencies>
##### 代码示例
package com.netease.utiludf;

import java.util.UUID;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.udf.UDFType;
import org.apache.hadoop.io.Text;


@UDFType(deterministic = false)
public class UdfUtil extends UDF {
    private Text result = new Text();

    public Text evaluate(){
        result.set(UUID.randomUUID().toString().replaceAll("-", ""));
        return result;
    }
}

中台实践

上传udf包文件至hdfs

DEMO-hiveudf-不重复uuid - 图1

自主分析运行或离线开发
add jar hdfs://easyops-cluster/user/poc/udf/table_ddl/hiveuuid-1.0-SNAPSHOT.jar;
create temporary function uuid as "com.netease.utiludf.UdfUtil";
select  uuid(),* from poc.ads_lead_store_di  limit 10;

DEMO-hiveudf-不重复uuid - 图2


作者:qianzhaoyuan