天天看点

presto相关命令总结导出数据与hive不同点函数

导出数据

hive

相似

执行命令

注意语句不能换行。

./presto --server prestoMaster:7878 --catalog hive --schema sss --output-format CSV --execute " select * from hive.database.table "> /home/log.txt
           

执行sql文件

文件中的sql可以换行

./presto --server prestoMaster:7878 --catalog hive --schema sss --output-format CSV --file sql.sql > /home/log.txt
           
  • –server :本机的ip或者是主机名hostname,多台机器的时候为presto配置文件中 master的主机地址或主机名,7878为配置文件config.properties中http-server.http.port=7878的值。
  • –catalog : 为链接的数据库,(eg:hive)如果不配置则在sql语句中需要详细表来源。
  • –schema : 为查询用到的hive库中的数据库,类似于hive中的 database 。如果不配置则在sql语句中需要详细表来源。
  • –execute " " :执行的sql语句(不可换行)。
  • –output-format : 输出的格式,用的CSV格式。
  • –file : 执行sql文件。一定记住在sql后一定要有";",否则会出现Non-terminated statement: SELECT 错误。
  • >文件名:导出的路径+文件名。

hive

不同点

split

提取版本首位比较大小

cast(split('4.8.0','\\.')[0] as int) -- hive 
cast(split('4.8.0','.')[1] as int) --presto
           

nvl与coalesce

  • hive中如果第一个值为空则取第二个值,用

    nvl(A,B)

  • presto中采用

    coalesce(A,B)

时间戳转换日期时间

  • hive中直接用

    from_unixtime(timestamp, 'format')

    可以直接将时间戳转换成对应格式的日期时间
  • presto中则采用

    format_datetime(from_unixtime(timestamp),'format')

-- HIVE
select from_unixtime(1597719329,'yyyy-MM-dd HH:mm:ss')  -- 2020-08-18 10:55:29
-- PRESTO
select format_datetime(from_unixtime(1597719329),'yyyy-MM-dd HH:mm:ss') -- 2020-08-18 10:55:29
           

整数相除

presto中整数和整数相除,结果为整数。应该采用 a*1.00/b

函数

grouping sets

指定多个列进行分组,结果列中不属于分组列的将被设置为NUll。

eg:可以用来统计汇总函数

-- 不去重汇总
select coalesce(dt, '汇总') dt, sum(cnt) as cnt
from
  (select dt,count(distinct concat(channel, user_id)) as cnt from table group by dt) a
group by
  grouping sets((dt),());
  
-- 去重汇总
select coalesce(dt, '汇总') dt,count(distinct concat(channel, user_id)) as cnt 
from table 
group by
  grouping sets((dt),());
           

窗口函数

分析函数(如:sum(),max(),row_number()…) + 窗口子句(over函数)

常用

row_number() over()

来对样本进行排序

select row_number() over(partition by 字段 order by 字段 desc ) as rank
           

时间函数

当日日期
select current_date; -- yyyy-MM-dd
select format_datetime(current_date,'yyyyMMdd'); -- yyyyMMdd
           
日期-1天
select current_date - interval '1'  day ; -- yyyy-MM-dd
select cast(format_datetime(date_parse('20200819','%Y%m%d') - interval '1'  day,'yyyyMMdd') as varchar) ; -- yyyyMMdd
           
月/年初日期
select date_trunc('month',current_date); -- 月初日期
select date_trunc('year',current_date); -- 年初日期
           

间隔月份:当前月份-计算月份+1

8月2号-8月19号:1月

7月30号-8月19号:2月

继续阅读