zoukankan      html  css  js  c++  java
  • Hive创建一个简单的UDF

    创建一个类

    package com.dufeng.hive;
    
    import org.apache.commons.lang.StringUtils;
    import org.apache.hadoop.hive.ql.exec.UDF;
    import org.apache.hadoop.io.Text;
    
    /**
     * Hello world!
     *
     */
    public class Dufeng extends UDF {
        
        private Text result = new Text();
        
        public Text evaluate(Text str, String stripChars) {
            if (str == null) {
                return null;
            }
            
            result.set(StringUtils.strip(str.toString(), stripChars));
            return result;
        }
        
        
        public Text evaluate(Text str) {
            if (str == null) {
                return null;
            }
            
            result.set("hello " + StringUtils.strip(str.toString()));
            return result;
        }
    }

    用Maven构建成jar包

    maven clean install package

    打开hive shell控制台

    hive> add jar /home/hive/dufengHive-0.0.1-SNAPSHOT.jar;
    Added [/home/hive/dufengHive-0.0.1-SNAPSHOT.jar] to class path
    Added resources: [/home/hive/dufengHive-0.0.1-SNAPSHOT.jar]
    hive> list jars;
    /home/hive/dufengHive-0.0.1-SNAPSHOT.jar
    hive> create temporary function dufeng as 'com.dufeng.hive.Dufeng';
    OK
    Time taken: 0.382 seconds
    hive> select dufeng('sss') from employee;
    OK
    hello sss
    hello sss
    hello sss
    hello sss
    hello sss
    hello sss
    hello sss
    hello sss
    hello sss
    hive> select dufeng('hadoop', 'ha') from employee;
    OK
    doop
    doop
    doop
    doop
    doop
    doop
    doop
    doop
    doop
    Time taken: 0.178 seconds, Fetched: 9 row(s)


  • 相关阅读:
    maven+spark2.0.0最大连通分量
    Eclipse+maven+scala2.11.8+spark2.0.0的环境部署
    杀死mapreduce
    filter-自己的理解
    JS变量声明提升
    js==运算符强制转换规则
    html 文字间距
    如你所见,我开始用微博
    vue数据模拟
    vue项目目录介绍
  • 原文地址:https://www.cnblogs.com/yandufeng/p/6437163.html
Copyright © 2011-2022 走看看