使用 SPARK SQL 使用时间戳格式 '2016-11-22 14:35:51' 获取月份的周数

Pra*_*era 1 apache-spark apache-spark-sql

我有一个时间戳列,其中包含 SPARK SQL 中类型为“2016-11-22 14:35:51”的记录。有人能帮我检索给定时间戳格式的月份的周数吗?

我试过了,

SELECT timestamp, DATE_FORMAT(timestamp, 'u') AS WEEK FROM table_1;

但它给出了错误的输出,

timestamp  |  WEEK
2016-11-22 |  2
Run Code Online (Sandbox Code Playgroud)

感谢有人可以帮助我。

谢谢。

eli*_*sah 5

您使用了错误的文字,您需要使用W而不是u.

字面意思u是指星期几。

scala> sqlContext.sql("select current_timestamp() as time, date_format(current_timestamp,'W') as week").show
// +--------------------+----+
// |                time|week|
// +--------------------+----+
// |2017-01-09 13:46:...|   2|
// +--------------------+----+

scala> sqlContext.sql("select to_date('2017-01-01') as time, date_format(to_date('2017-01-01'),'W') as week").show
// +----------+----+
// |      time|week|
// +----------+----+
// |2017-01-01|   1|
// +----------+----+
Run Code Online (Sandbox Code Playgroud)

如果有更多疑问,可以随时参考Java中SimpleDateFormat官方文档