Hive-3.1.2(四)hive 函数,UDF自定义函数
2021-08-07 本文已影响0人
_大叔_
返回类型 | 函数名 | 描述 |
---|---|---|
int | length(string a) | 返回字符串a的长度 select length(a) from table_name |
string | reverse(string a) | 返回字符串a的反转结果 select reverse(a) from table_name |
string | concat(string a,string b...) | 字符串连接函数 select concat(id,name) from table_name |
string | concat_ws(string sep,string a,string b...) | 带分隔符的字符串连接函数 select concat_ws(',',id,name) from table_name |
string | substr(string a,index int,lastindex int) | 截取字符串 |
string | upper(string a) | 转大写 |
string | lower(string a) | 转小写 |
string | trim(string a) | 去两边空格 |
string | ltrim(string a) | 左边去空格 |
string | rtrim(string a) | 右边去空格 |
string | regexp_replace(string a,string b,string c) | 将字符串a中的符合java正则表达式b的部分替换为c,有些情况下要使用转义字符如 [] 为 [*],类似 oracle 的regexp_replace函数 |
string | regexp_extract(string a,string patten,int index) | 将字符串subject按照pattern正则表达式的规则拆分,返回index指定的字符 |
string | repeat(string a,int n) | 返回重复n次后的str字符串 |
array | split(string a,string pat) | 分割字符串函数 |
row | explode(array a) | 命令可以将一行数据,按指定规则切分多行 select explode(split(a,',')) from table_name |
regexp_replace
正则替换
hive> select regexp_replace('foobar','oo|ar','');
OK
fb
Time taken: 0.317 seconds, Fetched: 1 row(s)
regexp_extract
会提取 () 里的内容,下标从1开始,0默认全部
hive> select regexp_extract('foothebar','foo(.*)(bar)',1);
OK
the
Time taken: 0.307 seconds, Fetched: 1 row(s)
hive> select regexp_extract('foothebar','foo(.*)(bar)',2);
OK
bar
Time taken: 0.314 seconds, Fetched: 1 row(s)
hive> select regexp_extract('foothebar','foo(.*)(bar)',0);
OK
foothebar
Time taken: 0.658 seconds, Fetched: 1 row(s)
UDF
如果hive的内置函数不够用,我们也可以自定义函数来使用,这样的函数称为hive的用户自定义函数,简称UDF。
实现步骤
maven
<dependencies>
<!-- hive -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>3.1.2</version>
</dependency>
<!-- hadoop -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
实现
package com.example.demo;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
/**
* @author big uncle
* @date 2021/5/19 14:52
* 自定义一个函数,接收两个参数,如果参数1为 null值,则用第二个参数做为返回值
**/
public class Ifv extends GenericUDF{
/**
* 执行一次 检查参数个数 和 参数类型
**/
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
System.out.println("initialize 被调用了 ");
if(arguments.length != 2){
throw new UDFArgumentException("arg length must is 2");
}
// 返回一个String对象检查器
ObjectInspector outputOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
return outputOI;
}
/**
* 处理数据
**/
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
System.out.println("evaluate 被调用了");
if(arguments[0].get() != null){
return arguments[0].get().toString();
}
String arg1 = arguments[1].get().toString();
return arg1;
}
/**
* explain 详细计划
**/
@Override
public String getDisplayString(String[] children) {
System.out.println("getDisplayString 被调用了");
return getStandardDisplayString(getFuncName(), children);
}
}
添加到 hive,在hive中输入如下
add jar /root/udf-1.0.0.RELEASE.jar;
创建成一个函数
create temporary function ifv as 'com.example.demo.Ifv';
调用
hive> select ifv(null,"aaaa");
initialize 被调用了
evaluate 被调用了
initialize 被调用了
evaluate 被调用了
initialize 被调用了
evaluate 被调用了
OK
aaaa
Time taken: 6.55 seconds, Fetched: 1 row(s)
谁都不要在告诉我 initialize 只调用一次,mmp官网这么说我都不信