【问题标题】:converting dd-MMM-yy date format in Spark在 Spark 中转换 dd-MMM-yy 日期格式
【发布时间】:2017-07-24 15:26:22
【问题描述】:

我有一个 Spark 数据框 (Scala API),其中包含一个名为 transfer date 的列,日期格式为 string,格式为 24-JUL-17

我想将它转换为日期字符串为时间戳。我该怎么做?

【问题讨论】:

    标签: scala date apache-spark spark-dataframe


    【解决方案1】:

    我找到了:

    import org.apache.spark.sql.functions.unix_timestamp
    
    val ts = unix_timestamp($"transfer date", "dd-MMM-yy").cast("timestamp")
    dfs.withColumn("ts",ts).show()
    
    【解决方案2】:

    也可以用to_timestamp(date_col, expr)完成

    import org.apache.spark.sql.functions.to_timestamp
    
    val df = date.withColumn("ts", to_timestamp($"transfer date", "dd-MMM-yy"))
    

    现在 df 中的 ts 列是 timestamp 类型。

    【讨论】:

    • 我的数据框中有日期列,例如 ...01-01-2012 、 31-01-2012 ,如何转换为 01-Jan-2012 、 31-Jan-2012 格式?如何过滤这两个日期之间的数据???
    猜你喜欢
    • 2019-11-28
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多