您的位置:首页 > 其它

hive下UDF函数的使用

2016-09-08 23:49 597 查看
1、编写函数

package com.example.hive.udf;

import org.apache.hadoop.hive.ql.exec.UDF;

import org.apache.hadoop.io.Text;

public final class LowerCase extends UDF {

public Text evaluate(final Text s) {

if (s == null) { return null; }

return new Text(s.toString().toLowerCase());

}

}

package com.example.hive.udf;

import org.apache.hadoop.hive.ql.exec.UDF;

import org.apache.hadoop.io.Text;

public final class LowerCase extends UDF {

public Text evaluate(final Text s) {

if (s == null) { return null; }

return new Text(s.toString().toLowerCase());

}

}

2、用eclipse进行jar包导出

选中package 或者是java 文件夹,右击export jar

3、将导出的hiveudf.jar复制到hdfs上

hadoop fs -copyFromLocal hiveudf.jar hiveudf.jar

4、进入hive,添加jar,

add jar hdfs://localhost:9000/user/root/hiveudf.jar

5、创建函数

临时的函数

CREATE TEMPORARY FUNCTION my_lower AS ‘com.example.hive.udf.LowerCase’;

创建永久函数

CREATE FUNCTION tmp_db.json2map AS ‘brickhouse.udf.json.JsonMapUDF’ USING JAR ‘hdfs:///data/hiveudf/hivebrickhouseudf.jar’;

USING JAR 只能是hdfs路径下的jar包。

6、调用

select LowerCase(name) from teacher;

注:这种方法只能添加临时的函数,每次重新进入hive的时候都要再执行4-6,要使得这个函数永久生效,要将其注册到hive的函数列表

7、hive 查看函数

SHOW FUNCTIONS;

DESCRIBE FUNCTION ; 描述函数

DESCRIBE FUNCTION EXTENDED ; 更具体的描述函数如何使用

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-PermanentFunctions

https://cwiki.apache.org/confluence/display/Hive/HivePlugins
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: