定义:

UDF(User-Defined-Function),用户自定义函数对数据进行处理。

UDTF(User-Defined Table-Generating Functions) 用来解决 输入一行输出多行(On-to-many maping) 的需求。

UDAF(User Defined Aggregation Function)用户自定义聚合函数,操作多个数据行,产生一个数据行。

用法:

  1、UDF函数可以直接应用于select语句,对查询结构做格式化处理后,再输出内容。

  2、编写UDF函数的时候需要注意一下几点:

a)自定义UDF需要继承org.apache.hadoop.hive.ql.UDF。

b)需要实现evaluate函。

c)evaluate函数支持重载。

hive的本地模式:

  大多数的Hadoop job是需要hadoop提供的完整的可扩展性来处理大数据的。不过,有时hive的输入数据量是非常小的。在这种情况下,为查询出发执行任务的时间消耗可能会比实际job的执行时间要多的多。对于大多数这种情况,hive可以通过本地模式在单台机器上处理所有的任务。对于小数据集,执行时间会明显被缩短。

  如此一来,对数据量比较小的操作,就可以在本地执行,这样要比提交任务到集群执行效率要快很多。

  配置如下参数,可以开启Hive的本地模式:

hive> set hive.exec.mode.local.auto=true;(默认为false)


  当一个job满足如下条件才能真正使用本地模式:
    1.job的输入数据大小必须小于参数:hive.exec.mode.local.auto.inputbytes.max(默认128MB)
    2.job的map数必须小于参数:hive.exec.mode.local.auto.tasks.max(默认4)
    3.job的reduce数必须为0或者1


hive 中窗口函数row_number,rank,dense_ran,ntile分析函数的用法

  示例数据:

1   a    10
2   a   12
3   b   13
4   b   12
5   a   14
6   a   15
7   a   13
8   b   11
9   a   16
10  b   17
11  a   14

  

  sql语句:

select id,
name,
sal,
rank()over(partition by name order by sal desc ) rp,
dense_rank() over(partition by name order by sal desc ) drp,
row_number()over(partition by name order by sal desc) rmp
from f_test

  结果:

10    b    17    1    1    1
3 b 13 2 2 2
4 b 12 3 3 3
8 b 11 4 4 4
9 a 16 1 1 1
6 a 15 2 2 2
11 a 14 3 3 3
5 a 14 3 3 4
7 a 13 5 4 5
2 a 12 6 5 6
1 a 10 7 6 7

  ntile

    ntile(n),用于将分组数据按照顺序切分成n片,返回当前切片值
    ntile不支持rows between,比如 ntile(2) over(partition by cookieid order by createtime rows between 3 preceding and current row)
    如果切片不均匀,默认增加第一个切片的分布

  比如需求为:求sal前50%的人

select * from (
select id,
name,
sal,
NTILE(2) over(partition by name order by sal desc ) rn
from f_test
) t where t.rn=1

Hive已定义函数介绍:

  1、字符串长度函数:length

    语法: length(string A)
    返回值: int
  举例:

hive> select length(‘abcedfg’) from dual;

  2、字符串反转函数:reverse

  语法: reverse(string A)
  返回值: string
  说明:返回字符串A的反转结果

  举例:

hive> select reverse(‘abcedfg’) from dual;
gfdecba

  3、字符串连接函数:concat

    语法: concat(string A, string B…)
    返回值: string
    说明:返回输入字符串连接后的结果,支持任意个输入字符串

  举例:

hive> select concat(‘abc’,'def’,'gh’) from dual;
abcdefgh

  4、带分隔符字符串连接函数:concat_ws

    语法: concat_ws(string SEP, string A, string B…)
    返回值: string
    说明:返回输入字符串连接后的结果,SEP表示各个字符串间的分隔符
  举例:

 
hive> select concat_ws(‘,’,'abc’,'def’,'gh’) from dual;

abc,def,gh

  5、字符串截取函数:substr,substring

    语法: substr(string A, int start),substring(string A, int start)
    返回值: string
    说明:返回字符串A从start位置到结尾的字符串
  举例:

 
hive> select substr(‘abcde’,) from dual;

cde

hive> select substring(‘abcde’,) from dual;

cde

hive> select substr(‘abcde’,-) from dual; (和ORACLE相同)

e

  6、类型转换

    类型转换:case

select cast(1 as float); --1.0
select cast('2016-05-22' as date); --2016-05-22

    字符串转大写函数:upper,ucase

    字符串转小写函数:lower,lcase

    语法: lower(string A) lcase(string A)
    返回值: string
    说明:返回字符串A的小写格式
  举例:

hive> select lower(‘abSEd’) from dual;

absed

hive> select lcase(‘abSEd’) from dual;

absed

  7、左右去除空格函数

    左边去空格函数:ltrim

    右边去空格函数:rtrim

  8、正则表达式替换函数:regexp_replace

    语法: regexp_replace(string A, string B, string C)
    返回值: string
    说明:将字符串A中的符合java正则表达式B的部分替换为C。注意,在有些情况下要使用转义字符
  举例:

 hive> select regexp_replace(‘foobar’, ‘oo|ar’, ”) from dual;

fb
 

  9、正则表达式解析函数:regexp_extract

    语法: regexp_extract(string subject, string pattern, int index)
    返回值: string
    说明:将字符串subject按照pattern正则表达式的规则拆分,返回index指定的字符。注意,在有些情况下要使用转义字符
  举例:

 
hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, ) from dual;

the

hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, ) from dual;

bar

hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, ) from dual;

foothebar

  10、URL解析函数:parse_url,parse_url_tuple(UDTF)

    语法: parse_url(string urlString, string partToExtract [, string keyToExtract]),parse_url_tuple功能类似parse_url(),但它可以同时提取多个部分并返回
    返回值: string
    说明:返回URL中指定的部分。partToExtract的有效值为:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.
  举例:

 
hive> select parse_url(‘http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1′, ‘HOST’) from dual;

facebook.com

hive> select parse_url_tuple('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY:k1', 'QUERY:k2');

v1 v2

  11、json解析函数:get_json_object

    语法: get_json_object(string json_string, string path)
    返回值: string
    说明:解析json的字符串json_string,返回path指定的内容。如果输入的json字符串无效,那么返回NULL。
  举例:

 
hive> select get_json_object(‘{“store”:

> {“fruit”:\[{"weight":,"type":"apple"},{"weight":,"type":"pear"}],

> “bicycle”:{“price”:19.95,”color”:”red”}

> },

> “email”:”amy@only_for_json_udf_test.net”,

> “owner”:”amy”

> }

> ‘,’$.owner’) from dual;

amy

  12、集合查找函数: find_in_set

    语法: find_in_set(string str, string strList)
    返回值: int
    说明: 返回str在strlist第一次出现的位置,strlist是用逗号分割的字符串。如果没有找该str字符,则返回0(只能是逗号分隔,不然返回0)
  举例:

 
hive> select find_in_set(‘ab’,'ef,ab,de’) from dual;

hive> select find_in_set(‘at’,'ef,ab,de’) from dual;

  13、行转列:explode (posexplode Available as of Hive 0.13.0)

    说明:将输入的一行数组或者map转换成列输出
    语法:explode(array (or map))
  举例:

 
hive> select explode(split(concat_ws(',','','','','','','','','',''),',')) from test.dual;

  14、多行转换:lateral view

    说明:lateral view用于和json_tuple,parse_url_tuple,split, explode等UDTF一起使用,它能够将一行数据拆成多行数据,在此基础上可以对拆分后的数据进行聚合。
  举例:

    假设我们有一张表pageAds,它有两列数据,第一列是pageid string,第二列是adid_list,即用逗号分隔的广告ID集合:

string pageid  Array<int> adid_list
"front_page"  [1, 2, 3]
"contact_page"  [3, 4, 5]

    要统计所有广告ID在所有页面中出现的次数。

    首先分拆广告ID:

SELECT pageid, adid
FROM pageAds LATERAL VIEW explode(adid_list) adTable AS adid;

                      执行结果如下:

string pageid  int adid
"front_page" 1
"front_page" 2
"front_page" 3
"contact_page" 3
"contact_page" 4
"contact_page" 5

  解释一下,from后面是你的表名,在表名后面加lateral view explode。。。(你的行转列sql) ,还必须要起一个别名,我这个字段的别名为sp。然后再看看select后面的 s.*,就是原表的字段,我这里面只有一个字段,且为X

  多个lateral view的sql类如:

select * from exampletable lateral view explode(col1) mytable1 as mycol1 lateral view explode(mycol1) mytable2 as mycol2;

  15、union结果集合并

    union将多个select语句的结果集合并为一个独立的结果集

create table dw_oute_numbs as
select 'step1' as step,count(distinct remote_addr) as numbs from ods_click_pageviews where datestr='2013-09-20' and request like '/item%'
union
select 'step2' as step,count(distinct remote_addr) as numbs from ods_click_pageviews where datestr='2013-09-20' and request like '/category%'
union
select 'step3' as step,count(distinct remote_addr) as numbs from ods_click_pageviews where datestr='2013-09-20' and request like '/order%'
union
select 'step4' as step,count(distinct remote_addr) as numbs from ods_click_pageviews where datestr='2013-09-20' and request like '/index%';
+---------------------+----------------------+--+
| dw_oute_numbs.step | dw_oute_numbs.numbs |
+---------------------+----------------------+--+
| step1         | 1029          |
| step2         | 1029          |
| step3         | 1028          |
| step4         | 1018          |
+---------------------+----------------------+--+

  抽取一行数据转换到新表的多列样例:

    http_referer是获取的带参数请求路径,其中非法字符用\做了转义,根据路径解析出地址,查询条件等存入新表中,

 
drop table if exists t_ods_tmp_referurl;

create table t_ ods _tmp_referurl as

SELECT a.*,b.*

FROM ods_origin_weblog a LATERAL VIEW parse_url_tuple(regexp_replace(http_referer, "\"", ""), 'HOST', 'PATH','QUERY', 'QUERY:id') b as host, path, query, query_id;

  复制表,并将时间截取到日:

drop table if exists t_ods_tmp_detail;

create table t_ods_tmp_detail as

select b.*,substring(time_local,,) as daystr,

substring(time_local,) as tmstr,

substring(time_local,,) as month,

substring(time_local,,) as day,

substring(time_local,,) as hour

From t_ ods _tmp_referurl b;

Hive的内置函数的更多相关文章

  1. [Hive_6] Hive 的内置函数应用

    0. 说明 Hive 的内置函数的基本操作 | 时间函数 | String 函数 | 条件语句 | explode | split | substring 1. 基本操作 查看函数 show func ...

  2. hive的内置函数和自定义函数

    一.内置函数 1.一般常用函数 .取整函数 round() 当传入第二个参数则为精度 bround() 银行家舍入法:为5时,前一位为偶则舍,奇则进. .向下取整 floor() .向上取整 ceil ...

  3. Hive学习之路 (九)Hive的内置函数

    数学函数 Return Type Name (Signature) Description DOUBLE round(DOUBLE a) Returns the rounded BIGINT valu ...

  4. hive中内置函数

    查看函数的详细使用方法 desc function extended 函数名 例如: 1).desc function extended locate locate(substr, str[, pos ...

  5. Hive内置函数和自定义函数的使用

    一.内置函数的使用 查看当前hive版本支持的所有内置函数 show function; 查看某个函数的使用方法及作用,比如查看upper函数 desc function upper; 查看upper ...

  6. [Hive - Tutorial] Built In Operators and Functions 内置操作符与内置函数

    Built-in Operators Relational Operators The following operators compare the passed operands and gene ...

  7. [转] Hive 内置函数

    原文见:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF 1.内置运算符1.1关系运算符 运算符 类型 说明 A ...

  8. Hive(六)内置函数与高级操作

    一内置函数 1 数学函数 Return Type Name (Signature) Description DOUBLE round(DOUBLE a) Returns the rounded BIG ...

  9. Hive 内置函数

    原文见:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF 1.内置运算符1.1关系运算符 运算符 类型 说明 A ...

随机推荐

  1. python schedule 任务调度

    python schedule可以简单处理一些日常提醒的工作,下面简要说一下用法: import schedule import time def job(): print("I'm wor ...

  2. Batch Normalization 笔记

    原理 BN的效果 Why BN works? 原理 输入层可以归一化,那么其他层也应该可以归一化.但是有个重要的问题,为什么要引入beta和gamma. 为什么要引入beta和gamma 不总是要标准 ...

  3. window搭建svn服务器,本地提交至服务器后,直接同步

    找到版本库目录(在安装svnserver时指定的目录),如下图指定了一个版本库的hooks 在其中创建post-commit.bat文件(可先创建post-cmmit.txt再修改后缀名为bat). ...

  4. DQN核心思想理解

    看过Deep learning(convolutional neural network),看过RL(Q-learning).但是在两者结合这一块一直弄不明白. 我的疑问在于一直不明白DL是怎样识别出 ...

  5. Maven高级应用--编译全模块包-dist包

    1. 在需要生成dist包的模块级别,新建文件夹xxx-xxxx-dist 2. 进入目录,新建pom.xml,建议copy 3. dependencies节点,把要编译成全局包的应用引入进来 < ...

  6. angular里forRoot的作用

    模块A是这样定义的 @NgModule({ providers: [AService], declarations: [ TitleComponent ], exports: [ TitleCompo ...

  7. UVa 1609 - Foul Play

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. Centos7 yum安装mysql

    参考此文档:http://www.jb51.net/article/116032.htm http://www.jb51.net/article/95399.htm 1.在官网下载mysql57-co ...

  9. PHP时间戳和日期相互转换(转载)

    在php中我们要把时间戳转换日期可以直接使用date函数来实现,如果要把日期转换成时间戳可以使用strtotime()函数实现,下面我来给大家举例说明. 1.php中时间转换函数 strtotime ...

  10. PHP数组 转 对象/对象 转 数组

    /** * 数组 转 对象 * * @param array $arr 数组 * @return object */ function array_to_object($arr) { if (gett ...