sparksql和postgresql常用的一些函数

sparksql

 1.from_unixtime将时间戳换算成当前时间,to_unix_timestamp将时间转化为时间戳

SELECT from_unixtime(0, 'yyyy-MM-dd HH:mm:ss');  1970-01-01 00:00:00
SELECT to_unix_timestamp('2016-04-08', 'yyyy-MM-dd'); 1460041200
 

2.将一列拼接的字符串转换为一行等同于regexp_split_to_table

explode(split(substr(orginal_berth,1,LENGTH (orginal_berth) - 1),'[|]'))   

需要注意的是以上函数不能使用sum、count等聚合操作。可以看做一个表在进行聚合操作。

3.substr截取字符串,substring_index

SELECT substring_index('www.apache.org', '.', 2);   www.apache

SELECT substring_index('www.apache.org', '.', -1);  org

 postgresql

1.一行数据中的多字段值根据连接符拼接 

concat_ws('||',aaa,bbb);  aaa||bbb

2.计算某个字段的长度,string类型的输入和输出

length("aaa") = '3'        字段::text    转化为string类型

3.case when then else end的语句的使用

sum(case when "sex" =1 then "population" else 0 end) as 男

4.时间和时间戳的相互转换

to_char(timestamp, text)text把时间戳转换成字串to_char(current_timestamp, 'HH12:MI:SS')

 

to_timestamp(text, text)timestamp把字串转换成时间戳to_timestamp('05 Dec 2000', 'DD Mon YYYY')
yyyy-MM-dd HH24-MI-SS

5. 将null替换为自定义值

coalesce(字段名,替换值)

6.删除表中数据

truncate table tablename

7.substring 截取任意长度的子字符串

substring(string [from int] [for int])

8.按照某个符号进行拆分形成一行

regexp_split_to_table(字段,拆分符)

9.split_part拆分取下标

split_part('a-b','-',1)  a

10.合并两个相反的字符串

WITH A AS (
    SELECT
        split_part('tianjin-shanghai', '-', 1) AS relation
),
 b AS (
    SELECT
        split_part('tianjin-shanghai', '-', 2) AS relation
) SELECT
    CASE
WHEN A .relation < b.relation THEN
    A .relation || b.relation
ELSE
    b.relation || A .relation
END re
FROM
    A,
    b

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值