• Hive UDF函数


    1.由于公司性质,需要编写一个对字段加密解密的函数。

    建立一个maven项目,导入jar包,跟环境的版本保持一致即可。

    dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-exec</artifactId>
      <version>1.1.12</version>
    </dependency>

    然后继承UDF即可

    import org.apache.hadoop.hive.ql.exec.Description;
    import org.apache.hadoop.hive.ql.exec.UDF;
    
    @Description(name = "IpToNum", value = "_FUNC_(ip) - Convert IPv4 to a num(long).")
    public class IpToNum extends UDF {
      public long evaluate(String ip) {
          String[] nums = ip.split("\.");
          return Long.parseLong(nums[3]) + Long.parseLong(nums[2]) * 256
             + Long.parseLong(nums[1]) * 65536 + Long.parseLong(nums[0]) * 16777216;
      }
    }

    evaluate方法的输入输出即是UDF函数的输入输出
    Description注解部分提供函数的帮助信息. 

    2.创建永久函数

    打成jar包(idea的打包即可)上传到hdfs文件里:hdfs://test.com:8020/hiveudf/hive-udf.jar

    CREATE FUNCTION ods.enbase64 AS 'com.mo9.udf.EncodeBase64' USING JAR 'hdfs://test.com:8020/hiveudf/hive-udf.jar'

    这个是属于ods的永久函数,别的库调用时要加上ods.enbase64

    3.创建临时函数

    create temporary function iptonum as 'com.mo9.udf.EncodeBase64' using jar 'hdfs://test.com:8020/hiveudf/hive-udf.jar'

    终端断开,此函数失效。

  • 相关阅读:
    Java反射机制DOME
    Contos7 装bcm4312无线网卡驱动
    windows8.1+centos7双系统(装完centos后无win8引导)
    request+response
    HTTP协议+servlet配置
    类加载和反射
    线程池+线程安全
    IO流之Properties(String键值对)+序列流+打印流+commons-IO(最终流程)
    IO流之字节流 +字符流 (复制文件)
    IO流
  • 原文地址:https://www.cnblogs.com/ChouYarn/p/7987113.html
Copyright © 2020-2023  润新知