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)


  • 相关阅读:
    公司的CMS参数
    Kafka 如何保证消息可靠性
    我来了
    spring解决乱码
    mybatis反向工程
    Unicode控制字符
    功能跟进记录
    创建IDataProvider实例
    腾讯2016研发工程师笔试题36车 6跑道 没有计时器 最少要几次取前三
    .net mvc下拉列表DropDownList控件绑定数据
  • 原文地址:https://www.cnblogs.com/yandufeng/p/6437163.html
Copyright © 2011-2022 走看看