Hive 使用积累

类型转化
数字和字符串

CAST(数字 as STRING)

时间相关

https://www.cppentry.com/bencandy.php?fid=117&id=201849
https://www.cnblogs.com/dcx-1993/p/11359139.html
时间戳格式化 from_unixtime(message.create_time,‘yyyy-MM-dd’)
字符串转时间戳 unix_timestamp(‘20201021’,‘yyyyMMdd’) 【生成的为短时间戳 类似 1458382610】
cast(unix_timestamp(‘20220101’,‘yyyy-MM-dd’)*1000 AS bigint) as newColumeName

from_unixtime(actiontime/1000,‘yyyy-MM-dd HH:mm:ss’) 【如果时间戳是毫秒】

  • dt 区间
    dt >= ‘20230201’ AND dt <=‘20230202’
字符串拼接

https://blog.csdn.net/muzieryueniao/article/details/77053307

字符串按照特别字符拆分

select demo_a,sub from table_name lateral view explode(split(demo_a,“/”)) t1 as sub;

a/b 会拆分为多行 a 和 b
第一行 a/b a
第二行 a/b b

截取 ID 字段前两位和截取后两位
  • 截取前两位:
substr(ID,0,2)
  • 截取后两位
substr(ID,-2,2)
批量处理 json

json_tuple 函数
json 字段 必须是标准的

select log.字段2, 新表名.json内部字段1,新表名.json内部字段2 as sendSum 
from log.表名 log 
lateral view json_tuple(log.json字段属性,'json内部字段1','json内部字段2') 新表名 
as json内部字段1,json内部字段2
where dt='20200324' and ctime='2020032418'

获取json中第1条数据

[{a1,b1,c1},{a2,b2,c2}]

select get_json_object(字段,'$[0]') from table
处理JSON 字符串中的单个属性

比如 表为 demo_table,列名为 jsonstr, 内容为 {“age”:20,“name”:“jecket”},要搜 name 属性,重要的是 “$.”符号

select get_json_object(demo_table.jsonstr,'$.name') as name
  from demo_table
hive 处理 null 值
null 值替换

当属性不存在就取默认值

select nvl(字段名,0)from 表
Hive 中 NULL 值判断

注意 NULL 不等同于 空字符串,空字符串用 =‘’ 或者 !=‘’
NULL 有两种方式

is not NULL
--或者
!='\\N'
hive case when

case 待判断属性 when ‘a’ then ‘成功’ when ‘b’ then ‘失败’ else ‘未知’ end as ‘result’

select 
case 属性值 when 'a' then 值 when 'B' then 值 else 默认值  end as thirdPartyIds
from 表名

case when 结合复杂条件
*** 注意,条件判断情况下,case 直接跟 when

CASE
  WHEN (condition1) THEN result1
  WHEN (condition2) THEN result2
  WHEN (condition3) THEN result3 
  WHEN (condition4) THEN result4
  ELSE result_default 
END AS attribute_name
hive if else

if(条件,成立值,不成立值)

 select if (条件,条件成立值,条件不成立值) as sendSum from 表明
hive 截取字符串

得到 k SQL,第一位为1


SELECT substr('Spark SQL', 5) 
hive 拼接字符串

如下,将 aaa 和 bbb 拼接到一起,得到 aaabbb

select concat('aaa','bbb');
hive 模糊搜索

比如下面,模糊搜索包含"你好"的字符串

like "%你好%"
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值