根据https://blog.csdn.net/helloHbulie/article/details/115376657继续学hive sql
hive的DQL查询语法
单表查询
where语句
select * from score where s_score < 60;
注意:小于某个值是不包含null的,如上查询结果会把s_score为null的行剔除
group by 分组
select s_id,avg(s_score) from score group by s_id;
分组后对数据进行筛选,使用having
select s_id,avg(s_score) avgscore from score group by s_id having avgscore > 85;
join连接
INNER JOIN 内连接:只有进行连接的两个表中都存在与连接条件相匹配的数据才会被保留下来
select * from techer t inner join course c on t.t_id = c.t_id; --inner 可省略
LEFT OUTER JOIN 左外连接:左边所有数据会被返回,右边符合条件的被返回
select * from techer t left join course c on t.t_id = c.t_id;-- outer可省略
RIGHT OUTER JOIN 右外连接:右边所有数据被返回,左边符合条件的被返回
select * from techer t left join course c on t.t_id = c.t_id,;
FULL OUTER JOIN 满外连接:将会返回所有表中符合条件的所有记录,如果任一表的指定字段没有符合条件的值的话,那么就是用null值替代
select * from techer t full join course c on t.t_id = c.t_id;
order by排序
全局排序,只会有一个reduce
ASC(ascend):升序(默认), DESC(descend):降序
select * from student s left join score sco on s.s_id = sco.s_id order by sco.s_score DESC;
sort by 局部排序
每个MapReduce内部进行排序,对全局结果集来说不是排序
设置reduce个数
set mapreduce.job.reduces=3;
查看设置reduce个数
set mapreduce.job.reduces;
查询成绩按照成绩降序排列
select * from score sort by s_score;
将查询结果导入到文件中(按照成绩降序排列)
insert overwrite local directory '/export/servers/hivedatas/sort' select * from score sort by s_score;
distribute by 分区排序
distribute by :类似MR中partition,进行分区,结合sort by 使用
设置reduce个数,将我们对应的s_id划分到对应的reduce中去
set mapreduce.job.reduce=7;
通过distribute by 进行数据的分区
select * from score distribute by s_id sort by s_score;
cluster by
当distribute by和sort by 字段相同时,可以使用cluster by方式
cluster by 除了具有distribute by 的功能外还兼具sort by的功能,但是排序只能是正序排序,不能指定排序规则
以下两种写法等价
select * from score cluster by s_id;
select * from score distribute by s_id sort by s_id;
注意:
1.order by 会对输入做全局排序,因此只有一个reducer,会导致当输入规模较大时,需要较长的计算时间
2.sort by不是全局排序,其在数据进入reduce前完成排序。因此,如果用sort by 进行排序,并且设置mapred.reduce.task>1,则sort by只保证每个reducer的输出有序,不保证全局有序
3.distribute by(字段)根据指定字段将数据分到不同的reducer,且分发算法是hash散列
4.cluster by(字段)除了具有distribute by 的功能外,还会对该字段进行排序。因此如果分桶和sort字段是同一时,此时cluster by = distribute by+sort by
Hive函数
聚合函数
hive支持count(),max(),min(),sum(),avg()等常用聚合函数
注意:聚合操作时要注意null值
count(*)包含null值,统计所有行数
count(id)不包含null值
min求最小值是不包含null,除非所有值都是null
avg求平均值也是不包含null
非空集合总体变量函数:var_pop
语法:var_pop(col)
返回值:double
说明:统计结果集中col非空集合的总体变量(忽略null)
非空集合样本变量函数:var_samp
语法:var_samp(col)
返回值:double
说明:统计结果集中col非空集合的样本变量(忽略null)
总体标准偏离函数:stddev_pop
语法:stddev_pop(col)
返回值:double
说明:该函数计算总体标准偏离,并返回总体变量的平方根,其返回值与var_pop函数的平方根相同
中位数函数:percentile
语法:percentile(BIGINT col,p)
返回值:double
说明:求准确的第pth个百分位数,p必须介于0和1之间,但是col字段目前只支持整数,不支持浮点数类型
关系运算
支持:等值(=)、不等值(!= 或 <>)、小于(<),小于等于(<=)、大于(>)、大于等于(>=)
空值判断(is null)、非空判断(is not null)
LIKE比较:LIKE
语法:A LIKE B
操作类型:strings
描述:如果字符串A或者字符串B为null,则返回null;如果字符串A符合表达式B的正则语法,则为true,否则为false;B中字符'_'表示任意单个字符,而字符'%'表示任意数量字符
JAVA的LIKE操作:RLIKE
语法:A RLIKE B
操作类型:strings
描述:如果字符串A或者字符串B为null,则返回null;如果字符串A符合JAVA正则表达式B的正则语法,则为true,否则为false;
REGEXP操作:REGEXP
语法:A REGEXP B
操作类型:strings
描述:功能与RLIKE相同
示例:select 1 from tableName where 'footbar' REGEXP '^f.*rs';
结果:1
数学运算
支持所有数值类型:加(+)、减(-)、乘(*)、除(/)、取余(%)、位与(&)、位或(|)、位异或(^)、位取反(~)
逻辑运算
支持:逻辑(and)、逻辑或(or)、逻辑非(not)
数值运算
取整函数:round
语法:round(double a)
返回值:BIGINT
说明:返回double类型的整数值部分(遵循四舍五入)
示例:select round(3.1415926) from tableName;
结果:3
指定精度取整函数:round
语法:round(double a,int d)
返回值:double
说明:返回指定精度d的double类型
示例:select round(3.1415926,4) from tableName;
结果:3.1416
向下取整函数:floor
语法:floor(double a)
返回值:BIGINT
说明:返回等于或小于该double变量的最大整数
示例:select floor(3.641) from tableName;
结果:3
向上取整函数:ceil
语法:ceil(double a)
返回值:BIGINT
说明:返回等于或者大于该double变量的最小整数
示例:select ceil(3.1415926) from tableName;
结果:4
取随机数函数:rand
语法:rand(),rand(int seed)
返回值:double
说明:返回一个0到1范围内的随机数。如果指定种子seed,则会等到一个稳定的随机数序列
示例1:select rand() from tableName; --每次指定此语句的结果都不同
示例2:select rand(100); --只要指定种子,每次执行此语句得到的结果都一样
自然指数函数:exp
语法:exp(double a)
返回值:double
说明:返回自然对数e的a次方
示例:select exp(2);
结果:7.38905609893065
以10为底对数函数:log10
语法:log10(double a)
返回值:double
说明:返回以10为底的a的对数
示例:select log10(100);
结果:2.0
幂运算函数:pow
语法:pow(double a,double p)
返回值:double
说明:返回a的p次幂
示例:select pow(2,4)
结果:16.0
开平方函数:sqrt
语法:sqrt(double a)
返回值:double
说明:返回a的平方根
示例:select sqrt(16);
结果:4.0
二进制函数:bin
语法:bin(BIGINT a)
返回值:string
说明:返回a的二进制代码表示
示例:select bin(7);
结果:111
十六进制函数:hex()、将十六进制转化为字符串函数:unhex()
进制转换函数:cov(bigint num,int from_base,int to_base)
说明:将数值num从from_base进制转化到to_base进制
绝对值函数:abs()、正取余函数:pmod()、正弦函数:sin()、反正弦函数:asin()、余弦函数:cos()、反余弦函数:acos()、positive函数:positive()、negative函数:negative()
条件函数
if函数:if
语法:if(boolean testCondition,T valueTrue,T valueFalseOrNull)
返回值:T
说明:当条件testCondition为TRUE时,返回valueTrue,否则返回valueFalseOrNull
示例:select if(1=2,100,200);
结果:200
非空查找函数:coalesce
语法:coalesce(T v1,T v2,...)
返回值:T
说明:返回参数中的第一个非空值,如果所有值都为null,那么返回null
示例:select coalesce(null,'100','20');
结果:100
条件判断函数:case when(两种写法,其一)
语法:case when a then b [when c then d] *[else e ] end
返回值:T
说明:如果a为TRUE,则返回b,如果c为TRUE则返回d,否则返回e
示例:select case when 1=2 then 'tom' when 2=2 then 'mary' else 'tim' end from tableName;
结果:mary
条件判断函数:case when(两种写法,其二)
语法:case a when b then c [when d then e] * [else f ]end
返回值:T
说明:如果a等于b,那么返回c,如果a等于d,你那么返回e。否则返回f
示例:select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end from tableName;
结果:mary
日期函数
获取当前UNIX时间戳函数:unix_timestamp
语法:unix_timestamp()
返回值:bigint
说明:获得当前时区的UNIX时间戳
示例:select unix_timestamp() from tableName;
UNIX时间戳转日期函数:from_unixtime
语法:from_unixtime(bigint unixtime,string format)
返回值:string
说明:转化UNIX时间戳到当前时区的时间格式
示例:select from_unixtime(1616906976,'yyyyMMdd') from tableName;
结果:20210328
日期转UNIX时间戳函数:unix_timestamp
语法:unix_timestamp(string date)
返回值:bigint
说明:转换格式为"yyyy-MM-dd HH:mm:ss"的日期到UNIX时间戳。如果转化失败,则返回0
示例:select unix_timestamp('2021-03-08 14:21:15') from tableName;
结果:1615184475
指定格式日期转UNIX时间戳函数:unix_timestamp
语法:unix_timestamp(string date,string pattern)
返回值:bigint
说明:转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0
示例:select unix_timestamp('2021-03-08 14:21:15','yyyy-MM-dd HH:mm:ss') from tableName;
结果:1615184475
日期时间转日期函数:to_date
语法:to_date(string timestamp)
返回值:string
说明:返回日期时间字段中的日期部分
示例:select to_date('2021-03-08 14:21:15') from tableName;
结果:2021-03-08
日期转年函数:year
语法:year(string date)
返回值:int
说明:返回日期中的年
日期转月函数:month
语法:month(string date)
返回值:int
说明:返回日期中的月份
日期转天函数:day
语法:day(string date)
返回值:int
说明:返回日期中的天
日期转小时函数:hour
语法:hour(string date)
返回值:int
说明:返回日期中的小时
日期转分钟函数:minute
语法:minute(string date)
返回值:int
说明:返回日期中的分钟
日期转秒函数:second
语法:second(string date)
返回值:int
说明:返回日期中的秒
日期转周函数:weekofyear
语法:weekofyear(string date)
返回值:int
说明:返回日期在当前的周数
日期比较函数:datediff
语法:datediff(string enddate,string startdate)
返回值:int
说明:返回结束日期减去开始日期的天数
日期增加函数:date_add
语法:date_add(string startdate,int days)
返回值:string
说明:返回开始日期startdate增加days后的日期
日期减少函数:date_sub
语法:date_sub(string startdate, int days)
返回值:string
说明:返回开始日期startdate减少days后的日期