zoukankan      html  css  js  c++  java
  • Hive 自定义函数

    hive 支持自定义UDF,UDTF,UDAF函数

    以自定义UDF为例:

    使用一个名为evaluate的方法

    package com.hive.custom;
    
    import org.apache.hadoop.hive.ql.exec.UDF;
    import org.apache.hadoop.io.IntWritable;
    
    public class XiaoUDF extends UDF {
    
        /**
         *  值加1000
         * @param i
         * @return val
         */
        public IntWritable evaluate(final IntWritable i) {
         int val= i.get();
         val+=1000;
            return new IntWritable(val);
          }
    }

    将写好的代码打为jar包,上传到服务器,或者hdfs

    add jar /root/udfxiao.jar;
    //add jar you.jar

    注册函数

     注册一个临时函数

    create temporary function fei  as 'com.hive.custom.XiaoUDF';
    //fei:注册的函数名
    //com.hive.custom.XiaoUDF 注册函数的全类名

    使用函数

    select fei(id) from test;

    注册永久函数

    create  function testdb.peng  as 'com.hive.custom.XiaoUDF';
    //testdb 注册永久函数的数据库

    从HDFS上注册函数

    CREATE FUNCTION fei AS 'com.hive.custom.XiaoUDF' USING JAR 'hdfs:///udfxiao.jar';
    // fei 注册的函数名
    //com.hive.custom.XiaoUDF  函数的全内名
    //hdfs:///udfxiao.jar   hdfs上根目录下的jar

    删除函数

    drop temporary function if exists fei;

     

  • 相关阅读:
    Python-Matplotlib 12 多图figure
    Python-Matplotlib 11 子图-subplot
    Python Day16
    Python Day15
    Python Day13-14
    Python Day12
    Python Day11
    Python Day9-10
    Python Day8
    Python Day8
  • 原文地址:https://www.cnblogs.com/jottings/p/8387352.html
Copyright © 2011-2022 走看看