HIVE-新增字段/修改字段/时间函数等常用函数

1、增加字段脚本

添加单个字段
Alter table  tableName add column columnName decimal(12,3) ;

//添加多个字段
Alter table tableName  add column (
A string COMMENT 'A', 
B  decimal(16, 6) COMMENT 'B',
C decimal(16, 6) COMMENT 'C',
D  decimal(16, 6) COMMENT 'D',
F decimal(16, 6) COMMENT 'F ');

2、修改字段类型

//字段A类型修改成长度16位,6小数数值类型
Alter table tableName  change column A  A  decimal(16,6) ;

3、时间函数

unix_timestamp() : 返回单位:秒

4、输入格式函数

select from_unixtime(1534839814);

5、其他日期函数

select from_unixtime(unix_timestamp(),'yyyy-MM-dd') today -- 今天
select trunc(from_unixtime(unix_timestamp(),'yyyy-MM-dd') ,'MM') firstday -- 当月第一天
select last_day(from_unixtime(unix_timestamp(),'yyyy-MM-dd')) lastday -- 当月最后一天
select add_months(trunc(from_unixtime(unix_timestamp(),'yyyy-MM-dd') ,'MM') ,1) next_month_firstday --下个月第一天

6、任务中时间
 

# 任务中使用 变量
substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).withMillisOfDay(0).plusMonths(-1).dayOfMonth().withMinimumValue().getMillis()),1,10)
# 当前月第一天0点0分0秒
substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).withMillisOfDay(0).dayOfMonth().withMinimumValue().getMillis()),1,10)

# schedule.exec.time
定义:计划执行时间的unix时间戳
数据格式demo: 1534839814392 (单位:毫秒)
# 备注:下述 substr 获取到的是 秒,索引位从1开始
# 最后 endTime 等是 1970到现在的秒 
# 当前月第一天0点0分0秒
curMonFirstDay                        substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).withMillisOfDay(0).dayOfMonth().withMinimumValue().getMillis()),1,10)
# 当前月最后一天0点0分0秒
curMonLastDay                        substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).withMillisOfDay(0).dayOfMonth().withMaximumValue().getMillis()),1,10)
# 上个月第一天0点0分0秒
preMonFirstDay                        substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).withMillisOfDay(0).plusMonths(-1).dayOfMonth().withMinimumValue().getMillis()),1,10)
# 上个月最后一天0点0分0秒
preMonLastDay                        substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).withMillisOfDay(0).plusMonths(-1).dayOfMonth().withMaximumValue().getMillis()),1,10)
# 下个月第一天0点0分0秒
nextMonFirstDay                        substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).withMillisOfDay(0).plusMonths(1).dayOfMonth().withMinimumValue().getMillis()),1,10)
# 下个月最后一天0点0分0秒
nextMonLastDay                        substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).withMillisOfDay(0).plusMonths(1).dayOfMonth().withMaximumValue().getMillis()),1,10)
# 当前时间:距离1970的秒数
endTime                                        substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).getMillis()),1,10)
# 当前时间:距离1970的秒数:往前推2分钟
endTime_before2min                substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).minusMinutes(2).getMillis()),1,10)
# 当前时间:距离1970的秒数:往前推10分钟
endTime_before10min                substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).minusMinutes(10).getMillis()),1,10)
# 当前时间:距离1970的秒数:往前推1小时
endTime_before1hour                substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).minusHours(1).getMillis()),1,10)
# 当前时间:距离1970的秒数:往前推5天
endTime_before5day                substr($(new("org.joda.time.DateTime", ${schedule.exec.time}).minusDays(120).getMillis()),1,10)

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值