zoukankan      html  css  js  c++  java
  • 自定义函数hello,并注册到hive源码中并重新编译

    1 编写自己的udf方法hello

    package cn.zhangjin.hive.udf;
    
    
    import org.apache.hadoop.hive.ql.exec.Description;
    import org.apache.hadoop.hive.ql.exec.UDF;
    import org.apache.hadoop.io.Text;
    
    
    /**
     * @author zj
     * @create 2019-02-22 17:51
     * 一个UDF: hello
     */
    
    
    @Description(name = "sayhello",
            value = "_FUNC_(input_str) - returns Hello : input_str ",
            extended = "Example:
     "
                    + "  > SELECT _FUNC_('wxk') FROM src LIMIT 1;
    "
                    + "  'Hello : wxk'
    ")
    public class hello extends UDF {
        public Text evaluate(Text input) {
            return new Text("Hello: " + input);
        }
    }  

    pom配置

        <url>http://maven.apache.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
            <hive.version>1.1.0-cdh5.7.0</hive.version>
        </properties>
    
        <repositories>
            <repository>
                <id>cloudera</id>
                <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
            </repository>
    
        </repositories>
    
        <!-- 设定插件仓库 -->
        <pluginRepositories>
    
            <pluginRepository>
                <id>jeesite-repos</id>
                <name>Jeesite Repository</name>
                <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            </pluginRepository>
    
        </pluginRepositories>
    
        <dependencies>
    
            <dependency>
                <groupId>org.apache.hadoop</groupId>
                <artifactId>hadoop-common</artifactId>
                <version>${hadoop.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.hive</groupId>
                <artifactId>hive-exec</artifactId>
                <version>${hive.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.hive</groupId>
                <artifactId>hive-jdbc</artifactId>
                <version>${hive.version}</version>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    

      

    2 下载hive源码

    参见:FunctionRegistry  
     
    3 自己修改代码
     (1)修改udf函数,并放入源码中 
    将hello.java  放入 hive-1.1.0-cdh5.7.0/ql/src/java/org/apache/hadoop/hive/ql/udf 文件夹中
    vi hello.java 
    将 package com.****.hello; 修改为 package org.apache.hadoop.hive.ql.udf;
    

    (2)修改FunctionRegistry.java 文件

    vi hive-1.1.0-cdh5.7.0/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java 
    文件头部 一长串 import 下添加,因为我们要吧这个UDF添加进去。
    import org.apache.hadoop.hive.ql.udf.hello;
     
    文件头部 static 块中添加  system.registerUDF("hello", hello.class, false);
    如下:
    static {
        system.registerGenericUDF("concat", GenericUDFConcat.class);
        system.registerUDF("hello", hello.class, false);
        system.registerUDF("substr", UDFSubstr.class, false);
    

      

    4 重新编译源码

      maven install 这里用的idea导入工程进行编译

    5 把编译好的jar上传

      重新部署 或者 只将 编译后的hive-exec-1.1.0-cdh5.7.0.jar 放到原来hive部署的位置即可。两种方式都可以!! 

      我这里选择的是只将 编译后的hive-exec-1.1.0-cdh5.7.0.jar 放到原来hive部署的位置即可

      

      上传到hive的lib包下面

      /mnt/software/hive-1.1.0-cdh5.7.0/lib

      

    6 重新启动hive

      

     查询内置函数

    hive> show functions ;

     发现hello已经注册进去了

    7 测试一下函数 没有问题

      

  • 相关阅读:
    CentOS 7拨号上网(ADSL & PPPoE)
    linux使用nmcli重新生成网卡配置文件
    Linux 内存缓存占用过大,Centos7设置定时清除buff/cache的脚本
    部署redis6.0 常见问题
    ssh 升级导致的hadoop 主备切换失败
    配置zookeeper的 ACL权限
    sqoop 创建跟mysql相同表结构的hive表报错
    vim中显示不可见字符
    supervisor 使用
    使用hive streaming 统计Wordcount
  • 原文地址:https://www.cnblogs.com/QuestionsZhang/p/10420076.html
Copyright © 2011-2022 走看看