Spark-SQL常用内置日期时间函数
yyyy-MM-dd
spark.sql("select current_date").show(false)
结果显示:
+--------------+
|current_date()|
+--------------+
|2020-11-25 |
+--------------+
yyyy-MM-dd HH:mm:ss.毫秒
spark.sql("select now()").show(false)
结果显示:
+-----------------------+
|current_timestamp() |
+-----------------------+
|2020-11-25 11:02:50.484|
+-----------------------+
now()
spark.sql("select current_timestamp").show(false)
spark.sql("select now()").show(false)
spark.sql("select year(now()) as year").show()
结果显示:
+-----------------------+
|current_timestamp() |
+-----------------------+
|2020-11-25 12:04:28.859|
+-----------------------+
+----+
|year|
+----+
|2020|
+----+
spark.sql("select now()").show(false)
spark.sql("select month(now()) as month").show()
结果显示:
+-----------------------+
|current_timestamp() |
+-----------------------+
|2020-11-25 12:07:44.976|
+-----------------------+
+-----+
|month|
+-----+
| 11|
+-----+
spark.sql("select now()").show(false)
spark.sql("select day(now()) as day").show()
spark.sql("select dayofmonth(now()) as dayofmonth").show()
结果显示:
+-----------------------+
|current_timestamp() |
+-----------------------+
|2020-11-25 12:09:45.178|
+-----------------------+
+---+
|day|
+---+
| 25|
+---+
+---+
|day|
+---+
| 25|
+---+
spark.sql("select now()").show(false)
spark.sql("select hour(now()) as hour").show()
结果显示:
+-----------------------+
|current_timestamp() |
+-----------------------+
|2020-11-25 12:12:58.917|
+-----------------------+
+----+
|hour|
+----+
| 12|
+----+
spark.sql("select now()").show(false)
spark.sql("select minute(now()) as minute").show()
结果显示:
+-----------------------+
|current_timestamp() |
+-----------------------+
|2020-11-25 12:14:47.916|
+-----------------------+
+------+
|minute|
+------+
| 14|
+------+
spark.sql("select second(2020-11-25 12:16:25.172) as second").show()
结果显示:
+------+
|second|
+------+
| 25|
+------+
spark.sql("select dayofyear(now()) as dayNum").show()
结果显示:
+------+
|dayNum|
+------+
| 330|
+------+
spark.sql("select weekofyear(now()) as weekNum").show()
结果显示:
+-------+
|weekNum|
+-------+
| 48|
+-------+
spark.sql("select date_format(now(),'u') as weekMany").show()
结果显示:
+--------+
|weekMany|
+--------+
| 3|
+--------+
spark.sql("select quarter(now())").show()
结果显示:
+------------------------------------------+
|quarter(CAST(current_timestamp() AS DATE))|
+------------------------------------------+
| 4|
+------------------------------------------+
"year"、"yy"、"month"、"mon"、"mm"
,否则返回 null spark.sql("select trunc('2020-11-25','YEAR') as year").show(false)
结果显示:
+----------+
|year |
+----------+
|2020-01-01|
+----------+
spark.sql("select unix_timestamp() as unix").show()
spark.sql("select unix_timestamp('2020-11-25','yyyy-MM-dd') as unix").show()
结果显示:
+----------+
| unix|
+----------+
|1606302965|
+----------+
+----------+
| unix|
+----------+
|1606233600|
+----------+
unix_timestamp
相同spark.sql("select to_unix_timestamp(now()) as unix").show()
spark.sql("select to_unix_timestamp('2020-11-25','yyyy-MM-dd') as unix").show()
yyyy-MM-dd HH:mm:ss
spark.sql("select from_unixtime('1606303452') as time").show()
结果显示:
+-------------------+
| time|
+-------------------+
|2020-11-25 19:24:12|
+-------------------+
spark.sql("select to_date('2020-11-25') as time").show()
spark.sql("select to_date('2020-11-25','yyyy-MM-dd') as time").show()
结果显示:
+----------+
| time|
+----------+
|2020-11-25|
+----------+
spark.sql("select months_between(now(),'2020-10-29') as monNum").show()
结果显示:
+----------+
| monNum|
+----------+
|0.89760581|
+----------+
spark.sql("select add_months('2020-11-11',1) as afterMonthTime").show()
结果显示:
+--------------+
|afterMonthTime|
+--------------+
| 2020-12-11|
+--------------+
spark.sql("select date_add(now(),3) as addDay").show()
spark.sql("select date_sub(now(),3) as addDay").show()
结果显示:
+----------+
| addDay|
+----------+
|2020-11-28|
+----------+
+----------+
| addDay|
+----------+
|2020-11-22|
+----------+
spark.sql("select datediff(now(),'2020-11-20') as diff").show()
结果显示:
+----+
|diff|
+----+
| 5|
+----+
spark.sql("select last_day('2020-10-20') as last_day").show()
结果显示:
+----------+
| last_day|
+----------+
|2020-10-31|
+----------+
spark.sql("select next_day('2020-11-20','we') as next_day").show()
结果显示:
+----------+
| next_day|
+----------+
|2020-11-25|
+----------+