窗口函数

窗口函数

not in
dwd_user_members
dm_member_info

新增会员
select
pl,
browser,
co
count(distinct mid)
from dwd_user_members a
left join dm_member_info b on b.mid=a.mid
where b.mid is null
;

Not in 这个是性能不好的,影响执行效果! 优化成左链接,再判断是否为空

insert into dm_member_info

show functions; 查看函数方法
desc function ucase; 方法详细解释

排名函数:
row_number():
没有并列,相同名次的顺序排
rank():
有并列,相同名次空位
dense_rank():
有并列,相同名次不空位

学生成绩管理系统
学生表
课程表
教师表
学生选课表
学生成绩表

dt name score

需求:
对每一次考试成绩按照成绩高低排序
select *
from stu_score
order by dt asc,score desc
;

set hive.exec.mode.local.auto=true;

需求:求每次考试的学生的排名情况
select *,
row_number() over(distribute by dt sort by score desc) rm,
rank() over(distribute by dt sort by score desc) rk,
dense_rank() over(distribute by dt sort by score desc) drk
from stu_score
;

需求:求每次考试的前三名
select *
from (
select *,
row_number() over(distribute by dt sort by score desc) rm,
rank() over(distribute by dt sort by score desc) rk,
dense_rank() over(distribute by dt sort by score desc) drk
from stu_score
) a
where a.drk <= 3
;

select *
from stu_score
order by dt asc,score desc
limit 3
;

select *
from (
select *,
row_number() over(partition by dt order by score desc) rm,
rank() over(partition by dt order by score desc) rk,
dense_rank() over(partition by dt order by score desc) drk
from stu_score
) a
where a.drk <= 3
;

需求:求出每次考试的最高分、最低分
select
dt,
max(score) as maxscore,
min(score) as minscore
from stu_score
group by dt
;

需求:求每个学员的成绩与最高分的差值
select
*,
a.maxscore - sc.score as subscore
from stu_score sc
join
(select
dt,
max(score) as maxscore,
min(score) as minscore
from stu_score
group by dt
) a on sc.dt = a.dt
;

select
*,
max(score) over(distribute by dt sort by score desc) as maxscore,
min(score) over(distribute by dt sort by score desc) as minscore
from stu_score
;

window子句:
unbounded preceding
–从第一行开始
2 preceding —
current row ----当前行
n following ----往后多少行
unbounded following --到结束行

select
*,
max(score) over(distribute by dt sort by score asc) as maxscore,
min(score) over(distribute by dt sort by score desc rows between unbounded preceding and unbounded following ) as minscore
from stu_score
;

select
*,
max(score) over(distribute by dt sort by score asc) as maxscore
from stu_score
;

求数据的第一个版本和最后一个版本的值
first_value:第一次的值
last_value:最后一次的值
ignore nulls忽略空值
select
*,
first_value(score ignore nulls) over(distribute by dt sort by score desc) as maxscore,
last_value(score) over(distribute by dt sort by score desc rows between unbounded preceding and unbounded following ) as minscore
from stu_score
;

需求:求学生每两次考试的成绩变化情况
select sc.*,a.score as upscore
from stu_score sc
left join (
select *
from stu_score
where dt = ‘2018-11-23’
) a on a.name = sc.name
where sc.dt = ‘2018-12-01’
;

lag(c,n,default):
lead:

select *,
lag(score,1,score) over(distribute by name sort by dt asc) as upcore
from stu_score
where dt = ‘2018-12-01’
;

lag(c,n,default)
lead()
lag 和lead 可以 获取结果集中,按一定排序所排列的当前行的上下相邻若干offset 的某个行的某个列(不用结果集的自关联);
lag ,lead 分别是向前,向后;
lag 和lead 有三个参数,第一个参数是列名,第二个参数是偏移的offset,第三个参数是 超出记录窗口时的默认值)

select distinct userid
from t
where (unix_timestamp(timestamp) - unix_timestamp(lag(timestamp,99,timestamp) over(distribute by userid sort by timestamp asc))) <= 300000
;

unix_timestamp函数用法
1、unix_timestamp() 得到当前时间戳
2、如果参数date满足yyyy-MM-dd HH:mm:ss形式,则可以直接unix_timestamp(string date) 得到参数对应的时间戳
3、如果参数date满足yyyy-MM-dd HH:mm:ss形式,则我们需要指定date的形式,在进行转换
unix_timestamp(‘2009-03-20’, ‘yyyy-MM-dd’)=1237532400

mysql> select UNIX_TIMESTAMP(‘2012-09-04 18:17:23’);±--------------------------------------+
| UNIX_TIMESTAMP(‘2012-09-04 18:17:23’) |
±--------------------------------------+
| 1346753843 |
±--------------------------------------+1 row in set (0.00 sec)

from_unixtime函数用法
语法:from_unixtime(t1,’yyyy-MM-dd HH:mm:ss’)
其中t1是10位的时间戳值,即1970-1-1至今的秒,而13位的所谓毫秒的是不可以的。
对于13位时间戳,需要截取,然后转换成bigint类型,因为from_unixtime类第一个参数只接受bigint类型。例如:
select from_unixtime(cast(substring(tistmp,1,10) as bigint),’yyyy-MM-dd HH’) tim ,count(*) cn from ttengine_hour_data where …

mysql> select FROM_UNIXTIME(1344954515,’%Y-%m-%d %H:%i:%S’);±----------------------------------------------+
| FROM_UNIXTIME(1344954515,’%Y-%m-%d %H:%i:%S’) |
±----------------------------------------------+
| 2012-08-14 22:28:35 |
±----------------------------------------------+1 row in set (0.00 sec)

HQL
Order by 和 sort by的区别
1、Order By

order by 排序出来的数据是全局有序的,但是只能有一个partition

2、Sort By

sort by 排序出来的数据是局部有序的,但是全局无序。即partition内部是有序的,但是partition与partition之间的数据时没有顺序关系的

行列转换
列转行 (对某列拆分,一列拆多行)
使用函数:lateral view explode(split(column, ‘,’)) newColumn

行转列 (根据主键,进行多行合并一列)
使用函数:concat_ws(’,’,collect_set(column))
说明:collect_list 不去重,collect_set 去重。 column 的数据类型要求是 string

Hive中if函数和Mysql中ifnull的转换
在mysql中,ifnull函数的用法,其表达式如下:
IFNULL(expr1,expr2)
如果 expr1 不是 NULL,IFNULL() 返回 expr1,否则它返回 expr2。IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境

在hive写HQL的时候,ifnull函数是不存在的,但是,可以使用if(col is not null, col, default_value)来实现ifnull的功能,如果col不为null,返回col的值,否则返回default_value

if(delivery_province IS NOT NULL ,delivery_province,‘0’)

hive函数
不等值比较: <>和!=

日期函数

  1. UNIX 时间戳转日期函数: from_unixtime
    语法: from_unixtime(bigint unixtime[, string format])
    返回值: string
    说明: 转化 UNIX 时间戳(从 1970-01-01 00:00:00 UTC 到指定时间的秒数)到当前时区
    的时间格式
    举例:
    hive> select from_unixtime(1323308943,‘yyyyMMdd’) from lxw1234;
    20111208

  2. 获取当前 UNIX 时间戳函数: unix_timestamp
    语法: unix_timestamp()
    返回值: bigint
    说明: 获得当前时区的 UNIX 时间戳
    举例:
    hive> select unix_timestamp() from lxw1234;
    1323309615

  3. 日期转 UNIX 时间戳函数: unix_timestamp
    语法: unix_timestamp(string date)
    返回值: bigint
    说明: 转换格式为"yyyy-MM-dd HH:mm:ss"的日期到 UNIX 时间戳。如果转化失败,则返
    回 0。
    举例:
    hive> select unix_timestamp(‘2011-12-07 13:01:03’) from lxw1234;
    1323234063

  4. 指定格式日期转 UNIX 时间戳函数: unix_timestamp
    语法: unix_timestamp(string date, string pattern)
    返回值: bigint
    说明: 转换 pattern 格式的日期到 UNIX 时间戳。如果转化失败,则返回 0。
    举例:
    hive> select unix_timestamp(‘20111207 13:01:03’,‘yyyyMMdd HH:mm:ss’) from lxw1234;
    1323234063

  5. 日期时间转日期函数: to_date
    语法: to_date(string timestamp)
    返回值: string
    说明: 返回日期时间字段中的日期部分。
    举例
    hive> select to_date(‘2011-12-08 10:03:01’) from lxw1234;
    2011-12-08

  6. 日期转年函数: year
    语法: year(string date)
    返回值: int
    说明: 返回日期中的年。
    举例:
    hive> select year(‘2011-12-08 10:03:01’) from lxw1234;
    2011
    hive> select year(‘2012-12-08’) from lxw1234;
    2012

  7. 日期转月函数: month
    语法: month (string date)
    返回值: int
    说明: 返回日期中的月份。
    举例:
    hive> select month(‘2011-12-08 10:03:01’) from lxw1234;
    12
    hive> select month(‘2011-08-08’) from lxw1234;
    8

  8. 日期转天函数: day
    语法: day (string date)
    返回值: int
    说明: 返回日期中的天。
    举例:
    hive> select day(‘2011-12-08 10:03:01’) from lxw1234;
    8
    hive> select day(‘2011-12-24’) from lxw1234;
    24

  9. 日期转小时函数: hour
    语法: hour (string date)
    返回值: int
    说明: 返回日期中的小时。
    举例:
    hive> select hour(‘2011-12-08 10:03:01’) from lxw1234;
    10

  10. 日期转分钟函数: minute
    语法: minute (string date)
    返回值: int
    说明: 返回日期中的分钟。
    举例:
    hive> select minute(‘2011-12-08 10:03:01’) from lxw1234;
    3

  11. 日期转秒函数: second
    语法: second (string date)
    返回值: int
    说明: 返回日期中的秒。
    举例:
    hive> select second(‘2011-12-08 10:03:01’) from lxw1234;
    1

  12. 日期转周函数: weekofyear
    语法: weekofyear (string date)
    返回值: int
    说明: 返回日期在当前的周数。
    举例:
    hive> select weekofyear(‘2011-12-08 10:03:01’) from lxw1234;
    49

  13. 日期比较函数: datediff
    语法: datediff(string enddate, string startdate)
    返回值: int
    说明: 返回结束日期减去开始日期的天数。
    举例:
    hive> select datediff(‘2012-12-08’,‘2012-05-09’) from lxw1234;
    213

  14. 日期增加函数: date_add
    语法: date_add(string startdate, int days)
    返回值: string
    说明: 返回开始日期 startdate 增加 days 天后的日期。
    举例:
    hive> select date_add(‘2012-12-08’,10) from lxw1234;
    2012-12-18

  15. 日期减少函数: date_sub
    语法: date_sub (string startdate, int days)
    返回值: string
    说明: 返回开始日期 startdate 减少 days 天后的日期。
    举例:
    hive> select date_sub(‘2012-12-08’,10) from lxw1234;
    2012-11-28

条件函数

  1. 非空查找函数: COALESCE
    语法: COALESCE(T v1, T v2, …)
    返回值: T
    说明: 返回参数中的第一个非空值;如果所有值都为 NULL,那么返回 NULL
    举例:
    hive> select COALESCE(null,‘100’,'50′) from lxw1234;
    100
  2. 条件判断函数:CASE
    语法: CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END
    返回值: T
    说明:如果 a 等于 b,那么返回 c;如果 a 等于 d,那么返回 e;否则返回 f
    举例:
    hive> Select case 100 when 50 then ‘tom’ when 100 then ‘mary’ else ‘tim’ end from
    lxw1234;
    mary
    hive> Select case 200 when 50 then ‘tom’ when 100 then ‘mary’ else ‘tim’ end from
    lxw1234;
    Tim
  3. 条件判断函数:CASE
    语法: CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END
    返回值: T
    说明:如果 a 为 TRUE,则返回 b;如果 c 为 TRUE,则返回 d;否则返回 e
    举例:
    hive> select case when 1=2 then ‘tom’ when 2=2 then ‘mary’ else ‘tim’ end from lxw1234;
    mary
    hive> select case when 1=1 then ‘tom’ when 2=2 then ‘mary’ else ‘tim’ end from lxw1234;
    Tom

字符串函数

  1. 字符 ascii 码函数:ascii
    语法: ascii(string str)
    返回值: int
    说明:返回字符串 str 中第一个字符的 ascii 码
    举例:
    hive> select ascii(‘ba’) from lxw1234;
    OK
    98

  2. base64 字符串
    语法: base64(binary bin)
    返回值: string
    说明:返回二进制 bin 的 base 编码字符串
    举例:
    hive> select base64(binary(‘lxw1234’)) from lxw1234;
    OK
    bHh3MTIzNA==

  3. 字符串连接函数:concat
    语法: concat(string A, string B…)
    返回值: string
    说明:返回输入字符串连接后的结果,支持任意个输入字符串
    举例:
    hive> select concat(‘abc’,'def’,'gh’) from lxw1234;
    abcdefgh

  4. 带分隔符字符串连接函数:concat_ws
    语法: concat_ws(string SEP, string A, string B…)
    返回值: string
    说明:返回输入字符串连接后的结果,SEP 表示各个字符串间的分隔符
    举例:
    hive> select concat_ws(’,’,‘abc’,‘def’,‘gh’) from lxw1234;
    abc,def,gh

  5. 数组转换成字符串的函数:concat_ws
    语法: concat_ws(string SEP, array)
    返回值: string
    说明:返回将数组链接成字符串后的结果,SEP 表示各个字符串间的分隔符
    举例:
    hive> select concat_ws(’|’,array(‘a’,‘b’,‘c’)) from lxw1234;
    OK
    a|b|c

  6. 小数位格式化成字符串函数:format_number
    语法: format_number(number x, int d)
    返回值: string
    说明:将数值 x 的小数位格式化成 d 位,四舍五入
    举例:
    hive> select format_number(5.23456,3) from lxw1234;
    OK
    5.235

  7. 字符串截取函数:substr,substring
    语法: substr(string A, int start),substring(string A, int start)
    返回值: string
    说明:返回字符串 A 从 start 位置到结尾的字符串
    举例:
    hive> select substr(‘abcde’,3) from lxw1234;
    cde
    hive> select substring(‘abcde’,3) from lxw1234;
    cde
    hive> select substr(‘abcde’,-1) from lxw1234; (和 ORACLE 相同)
    e

  8. 字符串截取函数:substr,substring
    语法: substr(string A, int start, int len),substring(string A, int start, int len)
    返回值: string
    说明:返回字符串 A 从 start 位置开始,长度为 len 的字符串
    举例:
    hive> select substr(‘abcde’,3,2) from lxw1234;
    cd
    hive> select substring(‘abcde’,3,2) from lxw1234;
    cd
    hive>select substring(‘abcde’,-2,2) from lxw1234;
    de
    版权所有: http://lxw1234.com

  9. 字符串查找函数:instr
    语法: instr(string str, string substr)
    返回值: int
    说明:返回字符串 substr 在 str 中首次出现的位置
    举例:
    hive> select instr(‘abcdf’,‘df’) from lxw1234;
    OK
    4

  10. 字符串长度函数:length
    语法: length(string A)
    返回值: int
    说明:返回字符串的长度
    举例:
    hive> select length(‘abc’) from lxw1234;
    OK
    3

  11. 字符串查找函数:locate
    语法: locate(string substr, string str[, int pos])
    返回值: int
    说明:返回字符串 substr 在 str 中从 pos 后查找,首次出现的位置
    举例:
    hive> select locate(‘a’,‘abcda’,1) from lxw1234;
    OK
    1
    hive> select locate(‘a’,‘abcda’,2) from lxw1234;
    OK
    5

  12. 字符串格式化函数:printf
    语法: printf(String format, Obj… args)
    返回值: string
    说明:将指定对象用 format 格式进行格式化.
    举例:
    hive> select printf("%08X",123) from lxw1234;
    OK
    0000007B

  13. 字符串转换成 map 函数:str_to_map
    语法: str_to_map(text[, delimiter1, delimiter2])
    返回值: map<string,string>
    说明:将字符串按照给定的分隔符转换成 map 结构.
    举例:
    hive> select str_to_map(‘k1:v1,k2:v2’) from lxw1234;
    OK
    {“k2”:“v2”,“k1”:“v1”}
    hive> select str_to_map(‘k1=v1,k2=v2’,’,’,’=’) from lxw1234;
    OK
    {“k2”:“v2”,“k1”:“v1”}

  14. 字符串转大写函数:upper,ucase
    语法: upper(string A) ucase(string A)
    返回值: string
    说明:返回字符串 A 的大写格式
    举例:
    hive> select upper(‘abSEd’) from lxw1234;
    ABSED
    hive> select ucase(‘abSEd’) from lxw1234;
    ABSED

  15. 字符串转小写函数:lower,lcase
    语法: lower(string A) lcase(string A)
    返回值: string
    说明:返回字符串 A 的小写格式
    举例:
    hive> select lower(‘abSEd’) from lxw1234;
    absed
    hive> select lcase(‘abSEd’) from lxw1234;
    absed

  16. 去空格函数:trim
    语法: trim(string A)
    返回值: string
    说明:去除字符串两边的空格
    举例:
    hive> select trim(’ abc ') from lxw1234;
    Abc

  17. 左边去空格函数:ltrim
    语法: ltrim(string A)
    返回值: string
    说明:去除字符串左边的空格
    举例:
    hive> select ltrim(’ abc ') from lxw1234;
    abc

  18. 右边去空格函数:rtrim
    语法: rtrim(string A)
    返回值: string
    说明:去除字符串右边的空格
    举例:
    hive> select rtrim(’ abc ') from lxw1234;
    Abc

  19. 正则表达式替换函数:regexp_replace
    语法: regexp_replace(string A, string B, string C)
    返回值: string
    说明:将字符串 A 中的符合 java 正则表达式 B 的部分替换为 C。注意,在有些情况下要
    使用转义字符,类似 oracle 中的 regexp_replace 函数。
    举例:
    hive> select regexp_replace(‘foobar’, ‘oo|ar’, ‘’) from lxw1234;
    fb

  20. 正则表达式解析函数:regexp_extract
    语法: regexp_extract(string subject, string pattern, int index)
    返回值: string
    说明:将字符串 subject 按照 pattern 正则表达式的规则拆分,返回 index 指定的字符。
    举例:
    hive> select regexp_extract(‘foothebar’, ‘foo(.?)(bar)’, 1) from lxw1234;
    The
    hive> select regexp_extract(‘foothebar’, 'foo(.
    ?)(bar)’, 2) from lxw1234;
    bar
    hive> select regexp_extract(‘foothebar’, ‘foo(.?)(bar)’, 0) from lxw1234;
    foothebar
    注意,在有些情况下要使用转义字符,下面的等号要用双竖线转义,这是 java 正则表
    达式的规则。
    select data_field,
    regexp_extract(data_field,’.
    ?bgStart\=([^&]+)’,1) as aaa,
    regexp_extract(data_field,’.?contentLoaded_headStart\=([^&]+)’,1) as bbb,
    regexp_extract(data_field,’.
    ?AppLoad2Req\=([^&]+)’,1) as ccc
    from pt_nginx_loginlog_st
    where pt = ‘2012-03-26’ limit 2;

  21. URL 解析函数:parse_url
    语法: parse_url(string urlString, string partToExtract [, string keyToExtract])
    返回值: 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
    lxw1234;
    facebook.com
    hive> select parse_url(‘http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1’, ‘QUERY’, ‘k1’)
    from lxw1234;
    v1

  22. 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”: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 lxw1234;
Amy

  1. 空格字符串函数:space
    语法: space(int n)
    返回值: string
    说明:返回长度为 n 的字符串
    举例:
    hive> select space(10) from lxw1234;
    hive> select length(space(10)) from lxw1234;
    10
  2. 重复字符串函数:repeat
    语法: repeat(string str, int n)
    返回值: string
    说明:返回重复 n 次后的 str 字符串
    举例:
    hive> select repeat(‘abc’,5) from lxw1234;
    abcabcabcabcabc
  3. 左补足函数:lpad
    语法: lpad(string str, int len, string pad)
    返回值: string
    说明:将 str 进行用 pad 进行左补足到 len 位
    举例:
    hive> select lpad(‘abc’,10,‘td’) from lxw1234;
    tdtdtdtabc
    注意:与 GP,ORACLE 不同,pad 不能默认
  4. 右补足函数:rpad
    语法: rpad(string str, int len, string pad)
    返回值: string
    说明:将 str 进行用 pad 进行右补足到 len 位
    举例:
    hive> select rpad(‘abc’,10,‘td’) from lxw1234;
    abctdtdtdt
  5. 分割字符串函数: split
    语法: split(string str, string pat)
    返回值: array
    说明: 按照 pat 字符串分割 str,会返回分割后的字符串数组
    举例:
    hive> select split(‘abtcdtef’,‘t’) from lxw1234;
    [“ab”,“cd”,“ef”]

汇总统计函数(UDAF)
sum,max,min,count,avg
集合去重数:collect_set
语法: collect_set (col)
返回值: array
说明: 将 col 字段进行去重,合并成一个数组。
举例:
hive> select cookie,ip from lxw1234;
cookie1 127.0.0.1
cookie1 127.0.0.1
cookie1 127.0.0.2
cookie1 127.0.0.3
hive> select cookie,collect_set(ip) from lxw1234 group by cookie;
cookie1 [“127.0.0.1”,“127.0.0.2”,“127.0.0.3”]

集合不去重函数:collect_list
语法: collect_list (col)
返回值: array
说明: 将 col 字段合并成一个数组,不去重
举例:
hive> select cookie,ip from lxw1234;
cookie1 127.0.0.1
cookie1 127.0.0.1
cookie1 127.0.0.2
cookie1 127.0.0.3
hive>select cookie,collect_list(ip) from lxw1234 group by cookie;
cookie1 [“127.0.0.1”,“127.0.0.1”,“127.0.0.2”,“127.0.0.3”]

表格生成函数 Table-Generating Functions (UDTF)

数组拆分成多行:explode
语法: explode(ARRAY)
返回值: 多行
说明: 将数组中的元素拆分成多行显示
举例:
hive> select explode(array(1,2,3)) from lxw1234;
OK
1
2
3
Map 拆分成多行:explode
语法: explode(Map)
返回值: 多行
说明: 将 Map 中的元素拆分成多行显示
举例:
hive> select explode(map(‘k1’,‘v1’,‘k2’,‘v2’)) from lxw1234;
OK
k2 v2
k1 v1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值