17. Spark Sql 日期函数的综合应用:

17.

Spark Sql 日期函数的综合应用:

https://www.cnblogs.com/feiyumo/p/8760846.html

https://www.obstkel.com/blog/spark-sql-date-functions

上述两个帖子,有日期函数的使用说明

下面说下重点,求出某一天对应年及月及日的指标,伪代码如下

//新增    day_of_year, week_of_year, month_of_year, day_of_week, day_of_month, days_of_month, days_of_year                      

#年的第几天
 .withColumn("day_of_year", dayofyear(from_unixtime(col("unix_time"), "yyyy-MM-dd")).cast(LongType)) 

#年的第几周                        
 .withColumn("week_of_year", weekofyear(from_unixtime(col("unix_time"), "yyyy-MM-dd")).cast(LongType))

#年的第几个月
 .withColumn("month_of_year", month(from_unixtime(col("unix_time"))).cast(LongType))

#一周的第几天(周一为第一天,未使用dayofmonth,该函数的每周第一天是周日)
 .withColumn("day_of_week", from_unixtime(col("unix_time"), "u").cast(LongType))

#月的第几天
 .withColumn("day_of_month", dayofmonth(from_unixtime(col("unix_time"))).cast(LongType))

#所在月份有几天
 .withColumn("days_of_month", datediff( last_day(trunc(from_unixtime(col("unix_time"), "yyyy-MM-dd"), "MM")), trunc(from_unixtime(col("unix_time"), "yyyy-MM-dd"), "MM")).cast(LongType) +1)

#所在年份有几天
 .withColumn("days_of_year", datediff( concat(year(from_unixtime(col("unix_time"), "yyyy-MM-dd")), lit("-12-31")), trunc(from_unixtime(col("unix_time"), "yyyy-MM-dd"), "year")).cast(LongType) +1 )

#所在月份对应季度的最后一天
.withColumn("quarter_date",             l        
                  last_day(concat(year(from_unixtime(col("unix_time"))).cast(StringType), 
                           lit("-"),
                           when(quarter(from_unixtime(col("unix_time"))) === 1, lit("03"))
                           .when(quarter(from_unixtime(col("unix_time"))) === 2, lit("06"))
                           .when(quarter(from_unixtime(col("unix_time"))) === 3, lit("09"))
                           .otherwise(lit("12")),
                           lit("-01"))
) 

补充一个函数的说明:

date_trunc函数用法,我参考了官网的说明,发现其示例有错误。

https://spark.apache.org/docs/2.3.0/api/sql/#date_trunc

date_trunc

date_trunc(fmt, ts) - Returns timestamp ts truncated to the unit specified by the format model fmt.
fmt should be one of ["YEAR", "YYYY", "YY", "MON", "MONTH", "MM", "DAY", "DD", "HOUR", "MINUTE", "SECOND", "WEEK", "QUARTER"]

正确用法

https://www.iteblog.com/archives/2336.html

 

 

此贴来自汇总贴的子问题,只是为了方便查询。

总贴请看置顶帖:

pyspark及Spark报错问题汇总及某些函数用法。

https://blog.csdn.net/qq0719/article/details/86003435

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值