| 返回類型 | 函數(shù)名 | 描述 |
|---|---|---|
| int ???? | length(string a) | 返回字符串a(chǎn)的長度 select length(a) from table_name |
| string | reverse(string a) | 返回字符串a(chǎn)的反轉(zhuǎn)結(jié)果 select reverse(a) from table_name |
| string | concat(string a,string b...) | 字符串連接函數(shù) select concat(id,name) from table_name |
| string | concat_ws(string sep,string a,string b...) | 帶分隔符的字符串連接函數(shù) select concat_ws(',',id,name) from table_name |
| string | substr(string a,index int,lastindex int) | 截取字符串 |
| string | upper(string a) | 轉(zhuǎn)大寫 |
| string | lower(string a) | 轉(zhuǎn)小寫 |
| string | trim(string a) | 去兩邊空格 |
| string | ltrim(string a) | 左邊去空格 |
| string | rtrim(string a) | 右邊去空格 |
| string | regexp_replace(string a,string b,string c) | 將字符串a(chǎn)中的符合java正則表達(dá)式b的部分替換為c,有些情況下要使用轉(zhuǎn)義字符如 [] 為 [*],類似 oracle 的regexp_replace函數(shù) |
| string | regexp_extract(string a,string patten,int index) | 將字符串subject按照pattern正則表達(dá)式的規(guī)則拆分,返回index指定的字符 |
| string | repeat(string a,int n) | 返回重復(fù)n次后的str字符串 |
| array | split(string a,string pat) | 分割字符串函數(shù) |
| row | explode(array a) | 命令可以將一行數(shù)據(jù),按指定規(guī)則切分多行 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
會(huì)提取 () 里的內(nèi)容,下標(biāo)從1開始,0默認(rèn)全部
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的內(nèi)置函數(shù)不夠用,我們也可以自定義函數(shù)來使用,這樣的函數(shù)稱為hive的用戶自定義函數(shù),簡稱UDF。
實(shí)現(xiàn)步驟
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>
實(shí)現(xiàn)
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
* 自定義一個(gè)函數(shù),接收兩個(gè)參數(shù),如果參數(shù)1為 null值,則用第二個(gè)參數(shù)做為返回值
**/
public class Ifv extends GenericUDF{
/**
* 執(zhí)行一次 檢查參數(shù)個(gè)數(shù) 和 參數(shù)類型
**/
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
System.out.println("initialize 被調(diào)用了 ");
if(arguments.length != 2){
throw new UDFArgumentException("arg length must is 2");
}
// 返回一個(gè)String對(duì)象檢查器
ObjectInspector outputOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
return outputOI;
}
/**
* 處理數(shù)據(jù)
**/
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
System.out.println("evaluate 被調(diào)用了");
if(arguments[0].get() != null){
return arguments[0].get().toString();
}
String arg1 = arguments[1].get().toString();
return arg1;
}
/**
* explain 詳細(xì)計(jì)劃
**/
@Override
public String getDisplayString(String[] children) {
System.out.println("getDisplayString 被調(diào)用了");
return getStandardDisplayString(getFuncName(), children);
}
}
添加到 hive,在hive中輸入如下
add jar /root/udf-1.0.0.RELEASE.jar;
創(chuàng)建成一個(gè)函數(shù)
create temporary function ifv as 'com.example.demo.Ifv';
調(diào)用
hive> select ifv(null,"aaaa");
initialize 被調(diào)用了
evaluate 被調(diào)用了
initialize 被調(diào)用了
evaluate 被調(diào)用了
initialize 被調(diào)用了
evaluate 被調(diào)用了
OK
aaaa
Time taken: 6.55 seconds, Fetched: 1 row(s)
誰都不要在告訴我 initialize 只調(diào)用一次,mmp官網(wǎng)這么說我都不信