Hive函数介绍
一些函数不太会,查了些资料,分享一下
Hive已定义函数介绍:
1、字符串长度函数:length
语法: length(string A)
返回值: int
举例:
[sql] view plain copy
hive> select length(‘abcedfg’) from dual;
7
2、字符串反转函数:reverse
语法: reverse(string A)
返回值: string
说明:返回字符串A的反转结果
举例:
[sql] view plain copy
hive> select reverse(‘abcedfg’) from dual;
gfdecba
3、字符串连接函数:concat
语法: concat(string A, string B…)
返回值: string
说明:返回输入字符串连接后的结果,支持任意个输入字符串
举例:
[sql] view plain copy
hive> select concat(‘abc’,'def’,'gh’) from dual;
abcdefgh
4、带分隔符字符串连接函数:concat_ws
语法: concat_ws(string SEP, string A, string B…)
返回值: string
说明:返回输入字符串连接后的结果,SEP表示各个字符串间的分隔符
举例:
[sql] view plain copy
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位置到结尾的字符串
举例:
[sql] view plain copy
hive> select substr(‘abcde’,3) from dual;
cde
hive> select substring(‘abcde’,3) from dual;
cde
hive> select substr(‘abcde’,-1) from dual; (和ORACLE相同)
e
6、字符串大小写转换
字符串转大写函数:upper,ucase
字符串转小写函数:lower,lcase
语法: lower(string A) lcase(string A)
返回值: string
说明:返回字符串A的小写格式
举例:
[sql] view plain copy
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。注意,在有些情况下要使用转义字符
举例:
[sql] view plain copy
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指定的字符。注意,在有些情况下要使用转义字符
举例:
[sql] view plain copy
hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 1) from dual;
the
hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 2) from dual;
bar
hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 0) 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.
举例:
[sql] view plain copy
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。
举例:
[sql] view plain copy
hive> select get_json_object(‘{“store”:
> {“fruit”:\[{"weight":8,"type":"apple"},{"weight":9,"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)
举例:
[sql] view plain copy
hive> select find_in_set(‘ab’,'ef,ab,de’) from dual;
2
hive> select find_in_set(‘at’,'ef,ab,de’) from dual;
0
13、行转列:explode (posexplode Available as of Hive 0.13.0)
说明:将输入的一行数组或者map转换成列输出
语法:explode(array (or map))
举例:
[sql] view plain copy
hive> select explode(split(concat_ws(',','1','2','3','4','5','6','7','8','9'),',')) from test.dual;
1
2
3
4
5
6
7
8
9
14、多行转换:lateral view
说明:lateral view用于和json_tuple,parse_url_tuple,split, explode等UDTF一起使用,它能够将一行数据拆成多行数据,在此基础上可以对拆分后的数据进行聚合。
举例:
[sql] view plain copy
hive> select s.x,sp from test.dual s lateral view explode(split(concat_ws(',','1','2','3','4','5','6','7','8','9'),',')) t as sp;
x sp
a 1
b 2
a 3
解释一下,from后面是你的表名,在表名后面加lateral view explode。。。(你的行转列sql) ,还必须要起一个别名,我这个字段的别名为sp。然后再看看select后面的 s.*,就是原表的字段,我这里面只有一个字段,且为X
多个lateral view的sql类如:
[sql] view plain copy
SELECT * FROM exampleTable LATERAL VIEW explode(col1) myTable1 AS myCol1 LATERAL VIEW explode(myCol1) myTable2 AS myCol2;
抽取一行数据转换到新表的多列样例:
http_referer是获取的带参数请求路径,其中非法字符用\做了转义,根据路径解析出地址,查询条件等存入新表中,
[sql] view plain copy
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;
复制表,并将时间截取到日:
[sql] view plain copy
drop table if exists t_ods_tmp_detail;
create table t_ods_tmp_detail as
select b.*,substring(time_local,0,10) as daystr,
substring(time_local,11) as tmstr,
substring(time_local,5,2) as month,
substring(time_local,8,2) as day,
substring(time_local,11,2) as hour
From t_ ods _tmp_referurl b;
Hive函数介绍的更多相关文章
- Hive函数以及自定义函数讲解(UDF)
Hive函数介绍HQL内嵌函数只有195个函数(包括操作符,使用命令show functions查看),基本能够胜任基本的hive开发,但是当有较为复杂的需求的时候,可能需要进行定制的HQL函数开发. ...
- 大数据入门第十一天——hive详解(三)hive函数
一.hive函数 1.内置运算符与内置函数 函数分类: 查看函数信息: DESC FUNCTION concat; 常用的分析函数之rank() row_number(),参考:https://www ...
- python strip()函数 介绍
python strip()函数 介绍,需要的朋友可以参考一下 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除 ...
- PHP ob_start() 函数介绍
ob_start() 函数介绍: http://www.nowamagic.net/php/php_ObStart.php ob_start()作用: http://zhidao.baidu.com/ ...
- hive函数参考手册
hive函数参考手册 原文见:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF 1.内置运算符1.1关系运算符 运 ...
- Python开发【第三章】:Python函数介绍
一. 函数介绍 1.函数是什么? 在学习函数之前,一直遵循面向过程编程,即根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复 ...
- row_number() OVER(PARTITION BY)函数介绍
OVER(PARTITION BY)函数介绍 开窗函数 Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个 ...
- select与poll函数介绍
select与poll函数介绍 在所有依从POSIX的平台上,select函数使我们可以执行I/O多路转接.传向select的参数告诉内核: 1)我们所关心的描述符 2)对于每个描述符我们所关心的状态 ...
- swift1.2语言函数和闭包函数介绍
swift1.2语言函数和闭包函数介绍 在编程中,随着处理问题的越来越复杂,代码量飞速增加.其中,大量的代码往往相互重复或者近似重复.如果不采有效方式加以解决,代码将很难维护. swift1.2语言函 ...
随机推荐
- Head First PHP&MySQl第二章代码
PHP: <html> <head> <title>外星人绑架了我--报道一起绑架</title> </head> <body> ...
- python3 修改大数据量excel内容
最好使用python3 64位 对excel的修改操作: from openpyxl import load_workbook import time #打开一个excel表格.xlsx wb = l ...
- VeryNginx中文文档
VeryNginx VeryNginx 是一个功能强大而对人类友好的 Nginx 扩展程序. 提示 v0.2` 版本之后,控制台入口被移动到了 `/verynginx/index.html 中文控制台 ...
- [转载]Linux软件包及dpkg\apt等方法
Linux软件安装 来源:https://segmentfault.com/a/1190000011200004?share_user=1030000007255638 一.安装包分类 在Linux平 ...
- js 向数组对象中添加属性和属性值
let resultList = [{"name":"a1"},{"name":"b1"}] resultList.fo ...
- SourceTree报错
1.SourceTree 拉取报错 Your local changes to the following files would be overwritten by merge:XXX 原因: 本 ...
- Malloc与Free不调用构造函数与析构函数
例子: #include "stdafx.h" #include <new> #include <iostream> using namespace std ...
- AIX中设备管理
1.AIX系统中的设备概述 逻辑设备文件 #ls -l /dev 空设备文件 #/dev/null 设备的状态:undefined.defined.available.stopp ...
- P5200 [USACO19JAN]Sleepy Cow Sorting 牛客假日团队赛6 D 迷路的牛 (贪心)
链接:https://ac.nowcoder.com/acm/contest/993/E 来源:牛客网 对牛排序 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- 单调队列优化DP || [Poi2014]Little Bird || BZOJ 3831 || Luogu P3572
题面:[POI2014]PTA-Little Bird 题解: N<=1e6 Q<=25F[i]表示到达第i棵树时需要消耗的最小体力值F[i]=min(F[i],F[j]+(D[j]> ...