函数"/>
Hive中常用函数
Hive 查看表数据条数
-
方法一:在hue界面的hive下找到相应的表,即可看到表的行数和大小等信息。
-
方法二:利用分区来查找,通过计算每个分区的数据量来汇总得到最终的数据量
select cout(id) from ods_access where month='12'union allselect cout(id) from ods_access where month='11'
- 数据量小的情况下采用count(id)直接进行查询
select count(id) from ods_access
- 采用查询元数据的方式
select * from TBLS where TBL_NAME='call_center';select a.TBL_ID, a.TBL_NAME, b.PARAM_KEY, b.PARAM_VALUE from TBLS as a join TABLE_PARAMS as b where a.TBL_ID = b.TBL_ID and TBL_NAME="web_sales" and PARAM_KEY="numRows";
临时表的使用场景
使用临时表的时候大多是基于其他查询的中间结果进行多次计算的场景,比如:我的三个查询结果都是基于某个前置的统计结果进行的,那个可以将之前的查询结果做成一个临时表,这样在后面查询的时候就可以避免进行重复的操作。
InnerJoin的使用场景
inner join 使用的时候主要是基于笛卡尔积进行操作才是用inner join ,当需要使用笛卡尔积进行运算的时候才使用innerjoin
row_number、rank、dense_rank 区别
a row_number rank dense_rank
A 1 1 1
C 2 2 2
D 3 3 3
B 4 3 3
E 5 5 4
F 6 6 5
G 7 7 6
row_number 不管排名是否有相同的,都按照顺序1,2,3……n
rank:排名相同的名次一样,同一排名有几个,后面排名就会跳过几次
dense_rank:排名相同的名次一样,且后面名次不跳过
Hive常用函数
日期函数:
- date_format 函数,用于日期转换,将给定原格式的日期转换成为目标格式
date_format(visitDate,'YYYY-MM') mnselect date_format('2017-01-01','yyyy-MM-dd HH:mm:ss'); --日期字符串必须满足yyyy-MM-dd格式
结果:2017-01-01 00:00:00
- date_add、date_sub函数(加减日期)
select date_add('2019-01-01',1); --字符串必须满足yyyy-MM-dd格式
结果:2019-01-02select date_sub('2019-01-01',1); --字符串必须满足yyyy-MM-dd格式
结果:2018-12-31
- current_date() 得到当前日期的字符串
select current_date();
结果:2020-01-01
- unix_timestamp() 得到当前日期的时间戳
select unix_timestamp();
结果:1577858367
- current_timestamp() 得到当前的时间字符串
select current_timestamp();
结果:2020-01-01 13:52:46.018
- unix_timestamp(‘日期字符串’,‘pattern’)把指定格式的日期转换成时间戳
select unix_timestamp('2020/01/01','yyyy/MM/dd');
结果:1577808000
- from_unixtime(‘bigint时间戳’,‘pattern’)将时间戳转换成为指定格式的日期字符串
select from_unixtime(1517725479,'yyyy-MM-dd HH:dd:ss');
select from_unixtime(1517725479,'yyyyMMdd');
结果:2018-02-04 14:04:3920180204
注意:通常把unix_timestamp 和from_unixtime 结合使用,先用unix_timestamp 将指定格式的日期字符串转换成为时间戳格式,然后再使用from_unixtime将时间戳转换成为目标的日期字符串格式
20171205转成2017-12-05
select from_unixtime(unix_timestamp(‘20171205’,‘yyyymmdd’),‘yyyy-mm-dd’) from dual;
- hour 返回日期中的天,minute、day、month、year的用法类似
select hour(‘2020-07-10 11:32:12’);
结果:11
- datediff (‘开始日期’,‘结束日期’) 返回开始日期减去结束日期的天数
select datediff(‘2020-04-09’,‘2020-04-01’);
结果:8
Hive中取最近30天数据
datediff(CURRENT_TIMESTAMP ,create_time)<=30
Hive中 两个日期相差多少小时
select (unix_timestamp(‘2018-05-25 12:03:55’) - unix_timestamp(‘2018-05-25 11:03:55’))/3600
其他常用函数:
- regexp_replace 字符串替换函数,将源字符替换成为目标字符
regexp_replace(visitDate,'/','-')
- explode爆炸函数,将数据炸开
select explode(score) as (course,score) from score;原数据格式:
score.name score.score
peck {"math":90,"english":100,"sport":80}
alice {"math":80,"english":99,"sport":85}
tom {"math":62,"english":99,"sport":88}转换后的格式:
select explode(score) as (course,score) from score;hive (default)> select explode(score) as (course,score) from score;
course score
math 90
english 100
sport 80
math 80
english 99
sport 85
math 62
english 99
sport 88
get_json_object (解析json)
get_json_object(param1,"$.param2")
- param1:需要被解析的json字段
- param2:数组就用 [0,1,2…] 0,1,2是数组对应的元素,遇若jsonObject直接用 ".key"取出想要获取的value。
处理jsonArray(json数组),如person表的pjson字段有数据:
[{"name":"Tom","sex":"男","age":"22"},{"name":"Jack","sex":"女"age":"32"}]
1、如果需要获取第一个json对象,hive语句如下
SELECT get_json_object(pjson,"$.[0]") FROM person;
得到第一个json对象
{"name":"Tom","sex":"男","age":"22"}
2、如果要获取第一个json对象中的name属性的值:
SELECT get_json_object(pjson,"$.[0].age") FROM person;
collect_set()
把同一个分组不同行的数据聚合成为一个集合,用下标可以取某一个
对于非group by字段,可以用Hive的collect_set函数收集这些字段,返回一个数组;
select course,collect_set(area),avg(score) from student group by course;result: chinese ["sh","bj"] 89math ["bj"] 90
日期处理函数
1.date_fromat()
select date_fromat('2019-02-19','yyyy-MM');result:2019-02
2.date_add()
select date_add('2019-02-10',-1);result:2019-02-09
3.next_day()
select next_day('2019-02-10','Mo');---取当前周的下一个周一result:2019-02-08select date_add(next_day('2019-02-10','Mo'),-7); ---取当前周的周一
4.last_day()
select last_day('2019-02-10');---取当前月份的最后一天result:2019-02-28
更多推荐
Hive中常用函数
发布评论