自定义函数hello,并注册到hive源码中并重新编译

1 编写自己的udf方法hello

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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:\n "
                + "  > SELECT _FUNC_('wxk') FROM src LIMIT 1;\n"
                + "  'Hello : wxk'\n")
public class hello extends UDF {
    public Text evaluate(Text input) {
        return new Text("Hello: " + input);
    }
}  

pom配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<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函数,并放入源码中 
1
2
3
将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 文件

1
2
3
4
5
6
7
8
9
10
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 测试一下函数 没有问题

 

  

 

posted @   Questions张  阅读(574)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· Blazor Hybrid适配到HarmonyOS系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
点击右上角即可分享
微信分享提示