我已经在火花中尝试过这种方式
df.withColumn(s"$new_col_name" unix_timestamp(date_col))
我想我拥有所有必需的库。
我实际上想出了三种方法:
.withColumn("new_col_name", (col("date_col_oftypeString").cast("date")))
.withColumn("new_col_name",lit($"date_col_oftypeString").cast(DateType))
.withColumn("new_col_name", to_date(unix_timestamp($"date_col_oftypeString", "yyyy-MM-dd").cast("timestamp")))
不要忘记导入:
import org.apache.spark.sql.functions._
import org.apache.spark.sql.types.{DateType}
import java.sql.Timestamp
import java.util._
import spark.implicits._