繁体   English   中英

在火花数据框中,如何使用 scala 将字符串类型的日期列转换为日期类型的日期列

[英]In spark Data frame how to convert Date column of type string to Date column of type Date using scala

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

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._

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM