我有一个数据框有两列(C,D)被定义为字符串列类型,但列中的数据实际上是日期 . 例如,列C的日期为"01-APR-2015",列D为"20150401"我想将这些更改为日期列类型,但我没有't find a good way of doing that. I look at the stack overflow I need to convert the string column type to Date column type in Spark SQL'的DataFrame . 日期格式可以是"01-APR-2015",我看this post但它没有与日期有关的信息
1 回答
Spark >= 2.2
你可以使用
to_date
:或
to_timestamp
:与中间
unix_timestamp
电话 .Spark < 2.2
从Spark 1.5开始,您可以使用
unix_timestamp
函数将字符串解析为long,将其转换为时间戳并截断to_date
:Note :
根据Spark版本,您可能需要进行一些调整,因为SPARK-11724:
如果使用未修补的版本
unix_timestamp
输出,则需要乘以1000 .