spark2.3 SQL内置函数——Date time functions

Date time functions 默认数据格式为yyyy-MM-dd格式

DataFrame数据

val df = Seq(
("A", "2019-01-10", "2019-05-02"),
("B", "2019-01-01", "2019-02-04"),
("D", "2019-01-09", "2019-03-02"))
.toDF("user_id", "start_time", "end_time")

1. defadd_months(startDate: Column, numMonths: Int)Column

def add_months 在月份上增加/减少n个月,其中正式表示增加,负数表示减少。

df.select(add_months(col("start_time"), 1).as("add_months")).show()

+----------+
|add_months|
+----------+
|2019-02-10|
|2019-02-01|
|2019-02-09|
+----------+  

2. def current_date()Column

获取当前时间的年月日

df.select(current_date()).show()

+--------------+
|current_date()|
+--------------+
|    2020-04-01|
|    2020-04-01|
|    2020-04-01|
+--------------+

3. def current_timestamp()Column

获取当前时间的时间戳

 df.select(current_timestamp()).show()

+--------------------------------------+
|      current_timestamp()               |
+--------------------------------------+
|2020-04-01T09:40:03.051+08:00 |
|2020-04-01T09:40:03.051+08:00 |
|2020-04-01T09:40:03.051+08:00 |
+--------------------------------------+

4. def date_add(start: Column, days: Int)Column

在天格式上增加天数

df.select(date_add(col("start_time"),1)).show()

+-----------------------+
|date_add(start_time, 1)|
+-----------------------+
|             2019-01-11|
|             2019-01-02|
|             2019-01-10|
+-----------------------+

5. def date_format(dateExpr: Column, format: String)Column

将date/timestamp/string,转化为第二个参数的格式形式

猜你喜欢

转载自www.cnblogs.com/yyy-blog/p/12610486.html