desc 表名 : 查看表结构
show partitions 表名 : 查看表的分区
distinct: 去重
show create table A : 快速得到建表语句命令
create table B as select * from A : 将原表A备份
一个B表出来进行测试
函数:
length() : 返回字符串长度
concat(A,B): 拼接AB
concat_ws('-','a','b'): 将a和b以下划线拼接,为a_b
reverse(): 字符串反转
函数
split(): 字符串分割
,返回分割后的字符串数组
split(reverse('a/b/c'),'/')[0] ------反转字符串后分割成数组取到第一个为c
substr() : 截取部分字符串
\
substr('2021ABCabc',0,3)
substring_index(org_full_path,'/',0-(size(split(org_full_path,'/'))-1)) 查询组织全路径下不包含一级组织id的剩余路径
trim(): 去除字符串左右空格
instr(string a, string b) :返回第二个参数b在待查找字符串a中的位置(找不到返回0)
repeat(string1,int1): 返回重复参数1字符串int1次后的字符串
union 和union all:
union:去重
union all: 不去重
(将2个查询结果union起来时,2结果SQL查询字段需相同)
例子:
select id ,name from A
union
select id,name from B
大小写转换:
lower():小写
upper():大写
cast(表达式 as 数据类型):`类型转换``
case when的用法:\
(一)
select title ,
case when id=1 then '北京'
when id=2 then '上海'
when id=3 then '广州'
else '深圳' end as city
from table;
(二)
case 条件判断函数, 当a为b时则返回c;当a为d时,返回e;否则返回f
case a when b then c when d then e else f end
判空函数:
coalesce():spark sql的判空
例子:
coalesce(name,'') : name字段为空则返回空字符串
coalesce(num,0) :num字段为空则返回0
nvl(a,b) :判空,a为空时,则取b值\
nvl(a,b,c) :a为空时返回c, a 不为空则返回b
爆炸函数:
explode(colname):将hive一行
中复杂的array或者map结构拆分成多行
,(适用于行转列的切换)
lateral view :将一行数据adid_list拆分为多行adid后,使用lateral view使之成为一个虚表adTable,使得每行的数据adid与之前的pageid一一对应, 因此最后pageAds表结构已发生改变,增加了一列adid。
例子:e
例子:
select pageid, adid from pageAds
lateral view explode(adid_list) adTable as adid
时间函数:
datediff(date1, date2):日期比较函数,返回相差天数
from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss') :返回当前时间
将时间格式---->时间戳
: select (1000 * unix_timestamp
("2021-01-01 00:00:00"));
将时间戳---->时间格式
:select from_unixtime
(cast(1627369405588/1000 as bigint);
开窗函数:
简单排名:
row_number() over(order by a desc)
:row_number 是行号,不会重复,如:1,2,3,4
rank() over(order by a desc)
:rank 是数据相同的,给出并列排行,但会跳跃
,如:1,2,2,4
dense_rank() over(order by a asc)
:dense_rank 类似rank,并列排名但不会跳跃
,如:1,2,2,3
分组排名 :
row_number() over(partition by
b order by a desc)
rank() over(partition by
b order by a desc)
dense_rank() over(partition by
b order by a asc)
--排名函数
- row_number() over(partition by ... order by ...) --分组排序
- rank() over(partition by ... order by ...) --分组排序
- dense_rank() over(partition by ... order by ...) --分组排序
--统计函数:
- count() over(partition by ... order by ...) --
分组计数
- max() over(partition by ... order by ...) --
分组取最大值
- min() over(partition by ... order by ...) --
分组取最小值
- sum() over(partition by ... order by ...) --
分组求和
- avg() over(partition by ... order by ...) --
分组取平均值
\
--取值函数:
- first_value() over(partition by ... order by ...) --取分组第一条
- last_value() over(partition by ... order by ...) --取分组最后一条\
--错行函数:
- lag(字段,1) over(partition by ... order by ...) --取出同一字段的
前1行的数据
,默认为1 - lead(字段,2) over(partition by ... order by ...) --取出同一字段的
后2行的数据
collect函数:
collect_list(不去重)
、collect_set(去重)
(列转行
)。
它们都是将分组中的某列转为一个数组返回
。
例子:
select a,b, concat_ws(',',collect_set(cast(c as string)))
from table group by a,b;
常用聚合统计函数:
count() : 条数 。
count(distinct) : 去重后的总数量
sum() :求和
min() : 最小
max() : 最大
avg() : 平均
数学函数:
rand() :随机数
round(double a) : 四舍五入取整
round(double a, int b) :四舍五入到指定小数位
bround(double a) :半偶数舍入模式,也称为高斯舍入或银行家舍入,如:bround(2.5)=2; bround(3.5)=4
bround(double a,int b) : 同上,
floor() :
替换函数:
regexp_replace:
full_outer_join: 左表或右表,其中一个表中存在匹配则返回行
yarn application -kill application_id : 流应用杀进程