
关系运算符
和where配合使用完成条件判断
查看函数方法
show functions ; desc function extended isnull;
关系运算符的方法
-- 空值判断 select '字段名' is null ; -- 模糊查询,以it开头的 select 'itcast' like 'it%'; -- 正则查询 \w,查看首位是不是字母,数字就用\d select 'iicast' rlike '\w\w'; select 'iicast' rlike 'ii.'; -- 算数运算符 -- 加减乘除 select 1+1; select 1/2; select 2 * 2; -- 取整取余 select 3 div 2; select 3 % 2; -- 位运算 -- 0 0000 / 1 0001 / 2 0010 / 3 0011 / 4 0100 / 5 0101 -- select 4 & 5; 相当于相乘 select 4 & 5; -- 逻辑运算符 与或非 select 1 > 1 and 1=1; select 1 > 1 or 1=1; select !1 > 1; -- 范围内判断 select 1 in (2,3); select 1 not in (2,3);
字符串 *** 作
-- 字符串长度函数
select length('asdf');
-- 字符串反转
select reverse('dfsf');
-- 字符串连接
select concat('dfs', 'dfs');
select concat_ws('-', 'dsf', 'dfs');
-- 字符串截取
select substr('sdfsdgd', 2, 4);
-- 字符串转大写函数
select upper('fdg');
select lower('FDGD');
-- 去除空格
select trim(' sdfsfd ');
select ltrim(' sfdf ');
select rtrim(' dfsf ');
-- 正则表达式替换函数
select regexp_replace('dfs334', '(\w)d', 'aaa');
-- URL解析网址
select parse_url('http://www.baidu.com', 'HOST');
-- 分割字符串函数
select split('ssdf,gfdd,dfgd',',');
时间日期函数
-- 获取当前日期
select `current_date`();
-- 获取当前时间戳
select `current_timestamp`();
-- 获取当前UNIX时间戳函数
select unix_timestamp();
-- UNIX时间戳转日期函数
select from_unixtime(3243534535);
-- -- 日期转UNIX时间戳函数
select unix_timestamp('2021-10-10 10:10:10');
-- 抽取日期函数
select to_date('2021-10-10 10:10:10');
-- 日期转年,转月,函数
select year('2021-10-10 10:10:10');
-- 日期比较函数
select datediff('2021-10-10 10:10:10','2021-10-12 10:10:10');
-- 日期增加函数
select date_add('2021-10-10 10:10:10',5);
select date_sub('2021-10-10 10:10:10',5);
数学函数
-- 取整函数 select round(3.34); -- 指定精度 select round(2.435,1); -- 向下取整 select `floor`(-1.1); -- 向上取整 select ceil(1.1); -- 进制转换函数 select conv(); -- 取随机数函数 select rand(2); -- 取决对值函数 select abs(-334);
集合函数
-- 集合元素个数
select size(`array`('a', 'r'));
-- 取map集合key函数
select map_keys(`map`('name','python','age',19));
-- 取map集合values函数
select map_values(`map`('name','python','age',19));
-- 判断数组是否包含指定元素
select array_contains(`array`(1,2,3),4);
类似于select 1 in (1,2,3);
-- 数组排序函数
select sort_array(`array`(1,5,3,2));
select sort_array(`array`('g','a','f','c'));
条件函数
-- if条件判断 select `if`(1>1, 'true', 'false'); -- 空值判断 select isnull(null); select isnotnull(null); -- 空值转换函数(如果是空返回aaa) select nvl(null, 'aaa'); -- 查找非空的函数 select coalesce(null, null , 3); -- 条件转换函数 case when select case when 1=1 then 'ok' end ; select count(case when 1=1 then 'ok' end);
类型转换
select cast('12' as int);
数据脱敏
-- 脱敏
select mask('dfs21342SDF');
select mask('dfs21342SDF', '*','%','#');
-- 对前n个字符进行脱敏替换
select mask_first_n('asd123',3);
-- 对后n个字符进行脱敏替换
select mask_last_n('itcastpython',3);
-- 除了前n个字符,其余的进行掩码处理
select mask_show_first_n('asdzxc',2);
select mask_show_last_n('qwertyu',2);
-- 返回字符串的hash编码
select hash('sdf');
select mask_hash('dfsgf');
-- SHA-1 加密
select sha('dfs');
select sha1('dgf');
select sha2('sdf', 2);
反射函数
-- 反射函数
select reflect("java.lang.Math","max",11,22);
自定义函数
– UDF:普通函数 输入一行输出一行
– UDAF:聚合函数 输入多行输出一行
– UDTF: 输入一行输出多行
HIVE函数进阶
使用explode对列进行拆分,用侧视图实现拼接
-- explode(UDTF)
create table the_nba_championship(
team_name string,
champion_year array
) row format delimited
fields terminated by ','
collection items terminated by '|';
select * from the_nba_championship;
select explode(champion_year) from the_nba_championship;
-- 侧视图实现
select a.team_name, b.year
from the_nba_championship as a lateral view explode(champion_year) b as year;
进行列转行
-- 列转行
create table row2col2(
col1 string,
col2 string,
col3 int
)row format delimited fields terminated by 't';
select * from row2col2;
-- 将col3列进行合并,进行行展示
select col1, col2, concat_ws(',', collect_list(cast(col3 as string)))
from row2col2
group by col1, col2;
把行转化为列
--把行转化为列
create table col2row2(
col1 string,
col2 string,
col3 string
)row format delimited fields terminated by 't';
select * from col2row2;
-- 用侧视图进行拆分
select
col1,
col2,
e.col3 as col3
from
col2row2 as c
lateral view
explode(split(c.col3, ',')) e as col3;
json格式数据处理
-- json数据处理
create table tb_json_test1 (
json string
);
select * from tb_json_test1;
-- get_json_object获取单个子段的json数据
select
--获取设备名称
get_json_object(json,"$.device") as device,
--获取设备类型
get_json_object(json,"$.deviceType") as deviceType,
--获取设备信号强度
get_json_object(json,"$.signal") as signal,
--获取时间
get_json_object(json,"$.time") as stime
from tb_json_test1;
-- json_tuple多个字段同时取值json数据
select
--解析所有字段
json_tuple(json,"device","deviceType","signal","time") as (device,deviceType,signal,stime)
from tb_json_test1;
json数据的自动处理
-- 自动指定hive自带的json处理方法 create table tb_json_test2 ( device string, deviceType string, signal double, `time` string ) ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe' STORED AS TEXTFILE; select * from tb_json_test2; -- 拓展xml数据的处理 desc function extended xpath;
窗口函数
##############
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)