在spark数据框中如何使用scala将字符串类型的Date列转换为Date类型的Date列

问题描述 投票:0回答:1

我已经在火花中尝试过这种方式

df.withColumn(s"$new_col_name" unix_timestamp(date_col))

我想我拥有所有必需的库。

string scala dataframe date apache-spark
1个回答
0
投票

我实际上想出了三种方法:

.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._
© www.soinside.com 2019 - 2024. All rights reserved.