Hive之UDFs(User-Defined Functions )

1.Hive之UDFs(User-Defined Functions )

1.1 Hive UDFs (User Defined Functions) 可以在SQL处理比较繁琐或者无法胜任时,解决比较复杂数据处理手段。

1.2 Hive的功能函数有哪些?

  • 内置操作(UDO)
  • 内置功能(UDF)
  • 内置聚合功能(UDAF)
  • 内置的表-生成功能(UDTF)
  • 自定义UDFs(本次主要是讲解自定义UDF)

2.为什么使用UDFs

2.1 当内置函数功能不足时使用

  • e.g. window 忽略了 null – Hive 不支持
  • e.g. 日期格式化功能 – Hive 1.2 才有更好的支持

2.2 当非过程性SQL无法完成任务时使用

  • e.g. 重复数据消除多对多时间序列配对
    A->B 10AM, A->C 11AM(消除), D->B 11AM(消除), D->C 11AM, E->C 12PM(消除)

3. 创建、删除、重载功能函数

3.1 创建临时功能函数

CREATE TEMPORARY FUNCTION function_name AS class_name;

3.2 删除临时功能函数

DROP TEMPORARY FUNCTION [IF EXISTS] function_name;

4. 永久功能函数

4.1 创建功能函数

在hive0.13之后可以将功能函数注册到元数据中,无需每次创建session重新创建临时功能函数。

CREATE FUNCTION [db_name.]function_name AS class_name
  [USING JAR|FILE|ARCHIVE 'file_uri' [, JAR|FILE|ARCHIVE 'file_uri'] ];

4.2 删除功能函数

DROP FUNCTION [IF EXISTS] function_name;

4.3 重载功能函数

RELOAD FUNCTION;

4.4 显示已定义的功能函数

SHOW FUNCTIONS "a.*";

5.自定义UDF

5.1 Java实例(MD5)

import com.google.common.hash.Hashing;
import org.apache.hadoop.hive.ql.exec.UDF;

public class Text2md5 extends UDF {
    public String evaluate(final String s) {
        if (s == null) { return null; }

        return Hashing.md5().hashUnencodedChars(s.trim()).toString();
    }

}

5.2 把class文件注册到临时功能函数

create temporary function tmp_md5 as 'com.example.hive.udf.Text2md5';

5.3 把class文件注册到永久功能函数(hive0.13之后)

create function test_hive.tmp_md5 as 'com.example.hive.udf.Text2md5';

5.4 (hive 0.13之前)加载自定义UDF Jar并注册创建UDF

指定jar路径

hive>add jar /home/acm/jar/demo-1.0.jar;

查看jar

hive>list jars

创建函数

CREATE TEMPORARY FUNCTION test_hive.tmp_md5 as 'com.example.hive.udf.Text2md5';

hive local模式可以直接在本地文件使用。
hive no-local 模式时,先将jar上传至HDFS后再使用。

5.5 (hive 0.13之后)UDF在创建函数语句时可以直接指定依赖的jar。

CREATE  TEMPORARY FUNCTION test_hive.test_md5 AS 'com.demo.hive.Text2md5' USING JAR 'hdfs:///tmp/test2/demo-1.0.jar';

(TEMPORARY为临时函数,关闭失去hive连接后临时函数自动去除;不带TEMPORARY则为注册永久函数)

5.6 查看功能函数

SHOW FUNCTIONS 'tes.*';

5.7 使用已注册的UDF

USE test_hive;

select id,test_hive.test_md5(name) from test limit 5;

5.8 问题以及解决方案

问题:创建udf
CREATE FUNCTION my_testmd5 AS ‘com.demo.hive.Text2md5’ USING JAR ‘/home/acm/jar/demo-1.0.jar’;

报错:FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.FunctionTask. Hive warehouse is non-local, but /home/acm/jar/demo-1.0.jar specifies file on local filesystem. Resources on non-local warehouse should specify a non-local scheme/path

先将jar上传到hdfs
hadoop fs -copyFromLocal 本地jar 目标jar

问题:udf无法使用
Caused by: java.lang.NoSuchMethodError: com.google.common.hash.HashFunction.hashUnencodedChars(Ljava/lang/CharSequence;)Lcom/google/common/hash/HashCode;
at com.demo.hive.Text2md5.evaluate(Text2md5.java:18)
… 23 more

问题:执行权限不足
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask. Permission denied: user=root, access=WRITE, inode="/user":hdfs:supergroup:drwxr-xr-x

解决:换hive用户

问题:FAILED: SemanticException Temporary function cannot be created with a qualified name.
CREATE TEMPORARY FUNCTION test_hive.t_md52 AS ‘com.demo.hive.Text2md5’ USING JAR ‘hdfs:///tmp/test-h/demo-1.0.jar’;

解决:CREATE TEMPORARY FUNCTION test_hive.t_md52 AS ‘com.demo.hive.Text2md5’ USING JAR ‘hdfs:///tmp/test-h/demo-1.0.jar’;
去掉 test_hive.

问题:/home/acm/jar/demo-1.0.jar does not exist
FAILED: Execution Error, return code -101 from org.apache.hadoop.hive.ql.exec.FunctionTask. /home/acm/jar/demo-1.0.jar does not exist
解决:上穿到hdfs上

你可能感兴趣的:(hive,UDFs,UDF,自定义函数,大数据,Hive)