版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Hive函数
函数/UDF
输入一行记录,输出一行记录
示例:upper/lower/length
聚集函数/UDAF
输入多行记录,输出一行记录
示例:sum/count/avg
表生成函数/UDTF
输入一行记录,输出多行记录
示例:explode
Hive函数——查看函数
SHOWFUNCTIONS;
DESCRIBEFUNCTION<function_name>;
DESCRIBEFUNCTIONEXTENDED<function_name>;
Hive内置函数——数学计算相关
返回类型
函数
描述
DOUBLE
round(DOUBLEa)
ReturnstheroundedBIGINTvalueofa.
BIGINT
floor(DOUBLEa)
ReturnsthemaximumBIGINTvaluethatisequaltoorless
thana.
BIGINT
ceil(DOUBLEa)
ReturnstheminimumBIGINTvaluethatisequaltoor
greaterthana.
DOUBLE
rand()
Returnsarandomnumber(thatchangesfromrowtorow)thatisdistributeduniformlyfrom0to1.Specifyingtheseedwillmakesurethegeneratedrandomnumbersequenceisdeterministic.
DOUBLE
sqrt(DOUBLEa)
Returnsthesquarerootofa.
DOUBLE
abs(DOUBLEa)
Returnstheabsolutevalue.
Hive内置函数——集合相关
返回类型
函数
描述
int
size(Map<K.V>)
Returnsthenumberofelementsinthemap
type.
int
size(Array<T>)
Returnsthenumberofelementsinthearray
type.
array<K>
map_keys(Map<K.V>)
Returnsanunorderedarraycontainingthekeys
oftheinputmap.
array<V>
map_values(Map<K.V>)
Returnsanunorderedarraycontainingthe
valuesoftheinputmap.
boolean
array_contains(Array<T>,value)
ReturnsTRUEifthearraycontainsvalue.
array<t>
sort_array(Array<T>)
Sortstheinputarrayinascendingorderaccordingtothenaturalorderingofthearray
elementsandreturnsit
Hive内置函数——日期相关
返回类型
函数
描述
bigint
unix_timestamp()
GetscurrentUnixtimestampinseconds.Thisfunctionisnon-deterministicandpreventsproperoptimizationofqueries-thishasbeendeprecatedsince2.0infavourofCURRENT_TIMESTAMPconstant.
bigint
unix_timestamp(stringdate)
Convertstimestringinformatyyyy-MM-ddHH:mm:sstoUnixtimestamp(inseconds),usingthedefaulttimezoneandthedefaultlocale,return0iffail:unix_timestamp('2009-03-2011:30:01')=1237573801
int
year(stringdate)
Returnstheyearpartofadateoratimestampstring:year("1970-01-
0100:00:00")=1970,year("1970-01-01")=1970.
int
quarter(date/timestamp/s
tring)
Returnsthequarteroftheyearforadate,timestamp,orstringinthe
range1to4(asofHive1.3.0).Example:quarter('2015-04-08')=2.
int
month(stringdate)
Returnsthemonthpartofadateoratimestampstring:month("1970-
11-0100:00:00")=11,month("1970-11-01")=11.
int
day(stringdate)
dayofmonth(date)
Returnsthedaypartofadateoratimestampstring:day("1970-11-01
00:00:00")=1,day("1970-11-01")=1.
Hive内置函数——字符串处理函数
返回类型
函数
描述
string
concat(string|binaryA,string|binaryB...)
Returnsthestringorbytesresultingfromconcatenatingthestringsorbytespassedinasparametersinorder.Forexample,concat('foo','bar')resultsin'foobar'.
int
length(stringA)
Returnsthelengthofthestring.
int
locate(stringsubstr,stringstr[,
intpos])
Returnsthepositionofthefirstoccurrenceofsubstrinstrafterpositionpos.
string
regexp_extract(stringsubject,stringpattern,intindex)
Returnsthestringextractedusingthepattern
String
replace(stringA,stringOLD,stringNEW)
ReturnsthestringAwithallnon-overlappingoccurrencesofOLDreplacedwithNEW.Example:selectreplace("ababab","abab","Z");returns"Zab".
String
substr(string|binaryA,intstart)substring(string|binaryA,intstart)
ReturnsthesubstringorsliceofthebytearrayofAstartingfromstartpositiontilltheendofstringA.Forexample,substr('foobar',4)resultsin'bar'
Hive内置聚集函数
返回类型
函数
描述
BIGINT
count(*),count(expr),count(DISTINCTexpr[,expr...])
count(*)-Returnsthetotalnumberofretrievedrows,includingrowscontainingNULLvalues.
count(expr)-Returnsthenumberofrowsforwhichthe
suppliedexpressionisnon-NULL.
DOUBLE
sum(col),sum(DISTINCTcol)
Returnsthesumoftheelementsinthegrouporthesumofthedistinctvaluesofthecolumninthegroup.
DOUBLE
avg(col),avg(DISTINCTcol)
Returnstheaverageoftheelementsinthegrouporthe
averageofthedistinctvaluesofthecolumninthegroup.
DOUBLE
min(col)
Returnstheminimumofthecolumninthegroup.
DOUBLE
max(col)
Returnsthemaximumvalueofthecolumninthegroup.
array
collect_set(col)
Returnsasetofobjectswithduplicateelementseliminated.
Hive内置表生成函数
返回类型
函数
描述
Nrows
explode(ARRAY)
Returnsonerowforeachelementfromthe
array.
Nrows
explode(MAP)
Returnsonerowforeachkey-valuepairfromtheinputmapwithtwocolumnsineachrow:oneforthekeyandanotherforthevalue.(AsofHive0.8.0.)
用户自定义函数
虽然Hive已经提供了很多内存的函数,但是还是不能,满足用户的需求,因此有提供了自定义函数供用户自己开发函数来满足自己的需求。
Java开发,生成Jar包
使用方式:
ADDJAR/full/path/to/yourjar;
CREATETEMPORARYFUNCTIONfunc_nameAS‘';
DROPTEMPORARYFUNCTIONIFEXISTSfunc_name;
UDF开发
继承UDF,重写evaluate方法即可
以length为例
UDF示例
importorg.apache.hadoop.hive.ql.exec.UDF;publicclassMD5HashextendsUDF{
publicStringevaluate(Stringin){
//请在此实现
}
}
UDAF开发
继承org.apache.hadoop.hive.ql.exec.UDAF
必须包含至少一个实现了org.apache.hadoop.hive.ql.exec.UDAFEvaluator的静态类
实现以下方法:
init():初始化计算函数和内部数据结构状态等。
iterate():每一个新值调用聚集计算时都会调用这个函数。计算函数要聚集计算的结果更新其内部状态
,iterate函数接受的参数和hive中被调用函数的参数是对应的。
terminatePartial():这个就是取计算到当前(局部)的时候的数据对象函数。(比如1-10。计算5的时候要调用一下这个函数查看一下当前的内部结构对象也就是1-5的聚合结果)
merge():在hive决定要合并一个部分聚集值和另一个部分聚集值是会调用merge()方法,该方法接受一个对象输入,这个对象的类型必须和terminatePartial()返回的一致。
terminate():hive需要最终聚集结果时会调用terminate方法,计算函数需要把状态作为一个值返回。
UDAF示例
importorg.apache.hadoop.hive.ql.exec.UDAF;
importorg.apache.hadoop.hive.ql.exec.UDAFEvaluator;importorg.apache.hadoop.io.IntWritable;
publicclassMaxNumberextendsUDAF{
publicstaticclassMaxNumberIntUDAFEvaluatorimp
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年法律职业资格考试民法夫妻共同财产卷含解析
- 2026高一生物上册第四单元第一次月考含答案及解析
- 《JBT 10545-2016平面移动类机械式停车设备》专题研究报告
- 《JBT 10369-2014液压手动及滚轮换向阀》专题研究报告
- 2026高二语文下册第一二三单元第一次月考含答案及解析
- 《点击音乐舞蹈英语(第四版)》课件 U10 A Happy Party
- 湖南中考:生物必考知识点总结
- 湖北中考:政治必考知识点大全
- 2026年基层政务公开条例知识测试题库
- 2026年斗争精神宣讲专项题库
- 一轮复习家长会课件
- 高中音乐-中国现当代音乐(2)教学课件设计
- 给水工程毕业设计模板
- 路灯安装质量评定表
- 07SG531钢网架设计图集-PDF解密
- 植物病害的诊断
- 儿科学 第七讲小儿单纯性肥胖症
- 派昂医药协同应用价值
- GB/T 24405.1-2009信息技术服务管理第1部分:规范
- GB/T 20474-2006玻纤胎沥青瓦
- 基础会计简答题及答案
评论
0/150
提交评论