hive常用函数

这篇具有很好参考价值的文章主要介绍了hive常用函数。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

行列转换

hive常用函数,hive,hadoop,数据仓库

create table tmp_summer1(id string,name string brith string);

insert into tmp_summer1 values('001','A','20211202');
insert into tmp_summer1 values('001','B','20211202');
insert into tmp_summer1 values('002','A','20211202');
insert into tmp_summer1 values('001','B','20211202');

hive常用函数,hive,hadoop,数据仓库

多行合并一行

--collect_set去重
-- 如果字段不是string类型则转: cast(c1 as string)
create table tmp_summer2 as 
select id
    ,concat_ws('&',collect_set(name)) as name
    ,max(brith) as brith
from tmp_summer1 a
group by id;       
 
--collect_list不去重          
select id
    ,concat_ws('&',collect_list(name)) as name
    ,max(brith) as brith
from tmp_summer1 a
group by id;  

hive常用函数,hive,hadoop,数据仓库

--列转行
create table tmp_summer3 as 
select id,name_split,brith
from tmp_summer2
lateral view explode(split(name,'&')) tmpTable as name_split

hive常用函数,hive,hadoop,数据仓库

--将一列拆分为多列
create table tmp_summer4 as 
select id
    ,max(case when name_split='A' then name_split else null end) as name_a
    ,max(case when name_split='B' then name_split else null end) as name_b
    ,max(case when name_split='C' then name_split else null end) as name_c
    ,max(brith) as brith
from tmp_summer3
group by id;

解析JSON

-- get_json_object(json_string, '$.key')
-- 功能:解析json的字符串json_string,返回key指定的内容。如果输入的json字符串无效,那么返回NULL。这个函数每次只能返回一个数据项。
SELECT 
GET_JSON_OBJECT('{"level":"2","time":1650973942596,"type":"0"}','$.level' ) as level ;

日期函数

2021-09-16 00:00:00        ——>        20210916
to_char(ZHTCJSJ,'yyyymmdd')    ——>   from_unixtime(unix_timestamp(ZHTCJSJ),'yyyyMMdd')

from_unixtime(unix_timestamp(date1),'yyyyMMdd')
-- 当前日期	
select current_date; --// 2021-12-23 
select unix_timestamp(); --// 1640224807
-- 建议使用current_timestamp,有没有括号都可以
select current_timestamp(); --// 2021-12-23 09:57:57.638 

-- 时间戳转日期
select from_unixtime(1505456567); --//2017-09-15 14:22:47
select from_unixtime(1505456567, 'yyyyMMdd'); --//20170915
select from_unixtime(1505456567, 'yyyy-MM-dd HH:mm:ss'); --// 2017-09-15 14:22:47

-- 日期转时间戳
select unix_timestamp('2019-09-15 14:23:00'); --//1568528580

-- 计算时间差
select datediff('2020-04-18','2019-11-21'); --//149
select datediff('2019-11-21', '2020-04-18'); --//-149

-- 查询该天是该月第几天
select dayofmonth(current_date); --//23
select dayofmonth('2021-12-23'); --//21

-- 计算月末:
select last_day(current_date); --//2021-12-31
select last_day('2021-12-15'); --//2021-12-31

-- 当月第1天:
select date_sub(current_date, dayofmonth(current_date)-1) --//2021-12-01
-- 下个月第1天:
select add_months(date_sub(current_date, dayofmonth(current_date)-1), 1);
 --//2022-01-01

-- 字符串转时间(字符串必须为:yyyy-MM-dd格式)
select to_date('2020-01-01');
select to_date('2020-01-01 12:12:12');

-- 日期、时间戳、字符串类型格式化输出标准时间格式
select date_format(current_timestamp(), 'yyyy-MM-dd HH:mm:ss');
select date_format(current_date(), 'yyyyMMdd');
select date_format('2020-06-01', 'yyyy-MM-dd HH:mm:ss');

-- 计算emp表中,每个人的工龄
select *, round(datediff(current_date, hiredate)/365,1) as workingyears 
from emp;

字符串函数

-- 转小写。lower
select lower("HELLO WORLD");
-- 转大写。upper
select lower(ename), ename from emp;

-- 求字符串长度。length
select length(ename), ename from emp;
-- 字符串拼接。 concat / ||
select empno || " " ||ename idname from emp;
select concat(empno, " " ,ename) idname from emp;

-- 指定分隔符。concat_ws(separator, [string | array(string)]+) 
SELECT concat_ws('.', 'www', array('lagou', 'com'));
select concat_ws(" ", ename, job) from emp;

-- 求子串。substr
SELECT substr('www.lagou.com', 5); 
SELECT substr('www.lagou.com', -5); 
SELECT substr('www.lagou.com', 5, 5);

-- 字符串切分。split,注意 '.' 要转义
select split("www.lagou.com", "\\.");

数学函数

-- 四舍五入。round
select round(314.15926); 
select round(314.15926, 2); 
select round(314.15926, -2);
-- 向上取整。ceil
select ceil(3.1415926);
-- 向下取整。floor
select floor(3.1415926);
-- 其他数学函数包括:绝对值、平方、开方、对数运算、三角运算等

条件函数

-- if (boolean testCondition, T valueTrue, T valueFalseOrNull)
select sal, if (sal<1500, 1, if (sal < 3000, 2, 3)) from emp;
-- CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END
-- 将emp表的员工工资等级分类:0-1500、1500-3000、3000以上
select sal, if (sal<=1500, 1, if (sal <= 3000, 2, 3)) from emp;
 
-- CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END -- 复杂条件用 case when 更直观
select sal, case when sal<=1500 then 1
                 when sal<=3000 then 2
                 else 3 end sallevel
from emp;
 
-- 以下语句等价
select ename, deptno,
       case deptno when 10 then 'accounting'
                   when 20 then 'research'
                   when 30 then 'sales'
                   else 'unknown' end deptname
from emp;
 
select ename, deptno,
       case when deptno=10 then 'accounting'
            when deptno=20 then 'research'
            when deptno=30 then 'sales'
            else 'unknown' end deptname
from emp;
 
-- COALESCE(T v1, T v2, ...)。返回参数中的第一个非空值;如果所有值都为 NULL,那么返回NULL
select sal, coalesce(comm, 0) from emp;
-- isnull(a) isnotnull(a)
select * from emp where isnull(comm);
select * from emp where isnotnull(comm);
-- nvl(T value, T default_value)
select empno, ename, job, mgr, hiredate, deptno, sal + nvl(comm,0) sumsal from emp;
-- nullif(x, y) 相等为空,否则为a
SELECT nullif("b", "b"), nullif("b", "a");

UDTF

User Defined Table-Generating Functions

用户定义表生成函数,一行输入,多行输出

-- explode,炸裂函数
-- 就是将一行中复杂的 array 或者 map 结构拆分成多行 
select explode(array('A','B','C')) as col; 
select explode(map('a', 8, 'b', 88, 'c', 888));
-- UDTF's are not supported outside the SELECT clause, nor nested in expressions
-- SELECT pageid, explode(adid_list) AS myCol... is not supported
-- SELECT explode(explode(adid_list)) AS myCol... is not supported
-- lateral view 常与 表生成函数explode结合使用
 
-- lateral view 语法:
--Lateral View 用于和UDTF函数【explode,split】结合来使用。
--首先通过UDTF函数将数据拆分成多行,再将多行结果组合成一个支持别名的虚拟表。
--主要解决在select使用UDTF做查询的过程中查询只能包含单个UDTF,不能包含其它字段以及多个UDTF的情况。
lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)*
fromClause: FROM baseTable (lateralView)*
 
-- lateral view 的基本使用
with t1 as (
  select 'OK' cola, split('www.lagou.com', '\\.') colb
)
select cola, colc from t1 lateral view explode(colb) t2 as colc;

窗口函数

窗口函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返回多行,而聚合函数对于每个组只返回一行

over() 开窗

select sum(age) from tb1;

错误: select id,sum(age) as sum_age from tb1;

--窗口函数是针对每一行数据的,如果over中没有参数,默认的是全部结果集
select id,sum(age) over() as sum_age from tb1;

--在over窗口中进行分区,对某一列进行分区统计,窗口的大小就是分区的大小
select id,sum(age) over(partition by id) as sum_age from tb1;

-- order by
select id
    ,sum(age) over(partition by id order by age) as sum_age 
from tb1;

--Window子句 rows between ... and ...
select id
    ,sum(age) over(partition by id order by age 
    between 1 preceding and 1 following) as sum_age 
from tb1;

hive常用函数,hive,hadoop,数据仓库

排名函数

row_number()。排名顺序增加不会重复;如1、2、3、4、… …

rank()。 排名相等会在名次中留下空位;如1、2、2、4、5、… …

dense_rank()。 排名相等会在名次中不会留下空位 ;如1、2、2、3、4、… …

select id,name,age
    ,row_number() over(partition by id order by age desc) as rank1
    ,rank() over(partition by id order by age desc) as rank2
    ,dense_rank() over(partition by id order by age desc) as rank3
from tb1;

序列函数

lag。返回当前数据行的上一行数据

lead。返回当前数据行的下一行数据

first_value。取分组内排序后,截止到当前行,第一个值

last_value。分组内排序后,截止到当前行,最后一个值

ntile。将分组的数据按照顺序切分成n片,返回当前切片值文章来源地址https://www.toymoban.com/news/detail-602204.html

select id, name, age,
       ntile(2) over(partition by id order by age) as ntile
from tb1;



with tmp as (
    select id, name, age,
       dense_rank() over (partition by id order by age desc) as rank
from tb1)
select id, score, rank,
       nvl(score - lag(score) over (partition by class order by score desc), 0) lagscore
  from tmp
 where rank<=3;      

with as

--WITH AS 语句可以为一个子查询语句块定义一个名称,功能类似临时表

-- with子句只能被select查询块引用
-- with子句的返回结果存到用户的临时表空间中,只做一次查询,反复使用,提高效率
-- 在同级select前有多个查询定义的时候,第1个用with,后面的不用with,并且用逗号隔开
-- 最后一个with 子句与下面的查询之间不能有逗号,只通过右括号分割,with 子句的查询必须用括号括起来

--相当于建了两个临时表a和b
with
a as (select * from tb1),
b as (select * from tb2)
select * from a, b where a.id = b.id;

到了这里,关于hive常用函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Hive常用的日期函数

    注意: current_timestamp() 获取的时UTC默认时区。 给定一个时间戳可基于 from_utc_timestamp/to_utc_timestamp 进行转换。 注意: nbsp;nbsp; 如果当前年的第一个周,天数超过3天,那就是当前年的第一周; nbsp;nbsp; 如果当前年的第一个周,天数小于等于3天,那就是上一年的最后一周。 wee

    2024年02月07日
    浏览(33)
  • hive常用函数

    多行合并一行 User Defined Table-Generating Functions 用户定义表生成函数,一行输入,多行输出 窗口函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返回多行,而聚合函数对于每个组只返回一行 row_number()。排名顺序增加不会重复;如1、2、3、4、… … ran

    2024年02月16日
    浏览(19)
  • Hive数据仓库---Hive的安装与配置

    Hive 官网地址:https://hive.apache.org/ 下载地址:http://www.apache.org/dyn/closer.cgi/hive/ 把安装文件apache-hive-3.1.2-bin.tar.gz上传到master节点的/opt/software目 录下,执行以下命令把安装文件解压到/opt/app目录中 进入/opt/app目录,为目录apache-hive-3.1.2-bin建立软件链接 即输入hive就相当于输入a

    2024年02月02日
    浏览(32)
  • hive数据仓库工具

    1、hive是一套操作数据仓库的应用工具,通过这个工具可实现mapreduce的功能 2、hive的语言是hql[hive query language] 3、官网hive.apache.org 下载hive软件包地址  Welcome! - The Apache Software Foundation https://archive.apache.org/ 4、hive在管理数据时分为元数据和真数据,其中元数据要保存在数据库中

    2024年02月04日
    浏览(25)
  • hive数据仓库课后答案

    一、 填空题 1.数据仓库的目的是构建面向     分析         的集成化数据环境。 2.Hive是基于     Hadoop         的一个数据仓库工具。 3.数据仓库分为3层,即      源数据层        、     数据应用层        和数据仓库层。 4.数据仓库层可以细分为      明细层

    2023年04月08日
    浏览(37)
  • Hive数据仓库简介

    Hive起源于Facebook,Facebook公司有着大量的日志数据,而Hadoop是实现了MapReduce模式开源的分布式并行计算的框架,可轻松处理大规模数据。然而MapReduce程序对熟悉Java语言的工程师来说容易开发,但对于其他语言使用者则难度较大。因此Facebook开发团队想设计一种使用SQL语言对日

    2024年02月15日
    浏览(43)
  • 安装hive数据仓库

    需要安装部署完成的Hadoop的环境如果不会搭建的可以参考: 卸载Centos7自带的mariadb mariadb-libs-5.5.64-1.el7.x86_64是使用 rpm -qa|grep mariadb 查询出来的名称 安装mysql 安装mysql时可能会出现的问题 1、依赖检测失败 问题很明显了就是依赖的问题,下载他说的依赖就好了 安装hive 上传并且

    2024年02月14日
    浏览(41)
  • 数据仓库 & Apache Hive

    目录 一、数据分析 1、数据仓库 1.1、数仓专注分析 1.2、数仓主要特征 1.3、数据仓库主流开发语言--SQL 2、Apache Hive 2.1、为什么使用Hive? 2.2、Hive和Hadoop关系 2.3、Hive架构图、各组件功能 2.4、Hive安装部署 2.4.1、Hive概念介绍 2.4.2、安装前准备     数据仓库 (英语:Data Warehous

    2024年01月22日
    浏览(38)
  • 【Hive】——数据仓库

    数据仓库(data warehouse):是一个用于存储,分析,报告的数据系统 目的:是构建面向分析的集成化数据环境,分析结果为企业提供决策支持 特点: 数据仓库本身不产生任何数据,其数据来源于不同外部系统 数据仓库也不需要消费任何的书,其结果开放给各个外部应用使用

    2024年02月04日
    浏览(29)
  • Hive数据仓库

    数据仓库(英语:Data Warehouse,简称数仓、DW),是一个用于存储、分析、报告的数据系统。 数据仓库的目的是构建面相分析的集成化数据环境,分析结果为企业提供决策支持(Decision Support)。 数据仓库本身并不“产生”任何数据,其数据来源不同外部系统; 同时数据仓库

    2024年02月15日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包