毫秒 date_format()
您可以使用
date_format()接受Java
SimpleDateFormat模式的Spark
SQL 。
SimpleDateFormat只能 使用模式“ S” 解析到毫秒。
import org.apache.spark.sql.functions._import spark.implicits._ //to use $-notation on columnsval df = tablereader1Df.withColumn("log_dt", date_format($"log_dt", "S"))更新:毫秒与Java 8的LocalDateTime
//importsimport java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import java.time.temporal.ChronoField;import org.apache.spark.sql.functions.udfval date_microsec = udf((dt: String) => { val dtFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.n") LocalDateTime.parse(dt, dtFormatter).getLong(ChronoField.MICRO_OF_SECOND)})检查:帮助建立DateTimeFormatter模式
使用
ChronoField.NANO_OF_SECOND代替
ChronoField.MICRO_OF_SECOND在UDF中获取纳秒。
val df = tablereader1Df.withColumn("log_date_microsec", date_microsec($"log_dt"))


