我在字串中有日期 Tue Dec 31 07:14:22 0000 2013 的格式,我需要將其轉換為 Date 物件,其中時間戳欄位將在 scala spark 中建立索引
uj5u.com熱心網友回復:
您可以通過按空格拆分字串列并將該列轉換為陣列型別,然后使用任何受支持的日期格式創建新的字串列來執行此操作。
//Creating sample data
import org.apache.spark.sql.functions._
val df = Seq(("Tue Dec 31 07:14:22 0000 2013"),("Thu Dec 09 09:14:42 0000 2017")).toDF("DateString")
//creating new column of type array from string column
val df1 = df.withColumn("DateArray", split($"DateString", " "))
//Getting the required elements from the array column and combining them to get the date
val df2 = df1.withColumn("DateTime" , concat($"DateArray".getItem(5), lit("-"), $"DateArray".getItem(1), lit("-"),$"DateArray".getItem(2))).withColumn("Date",to_date($"DateTime","yyyy-MMM-dd"))
//Using display to show the content of the dataframe. you can also use .show method.
display(df2)
您可以根據輸出要求洗掉不需要的列。
您可以看到如下輸出:

uj5u.com熱心網友回復:
為日期創建索引:
collection.createIndex(Indexes.ascending("date_column"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/355488.html
上一篇:當系統日期格式為dd/mm/yyyy時,MSAccess報告無法按日期欄位過濾
下一篇:日期時間年份超出范圍
