Scala:Spark SQL to_date(unix_timestamp)返回NULL

Sai*_*ung 5 scala apache-spark apache-spark-sql spark-dataframe spark-csv

Spark Version: spark-2.0.1-bin-hadoop2.7 Scala: 2.11.8

我正在将原始csv加载到DataFrame中.在csv中,虽然该列支持日期格式,但它们写成20161025而不是2016-10-25.该参数date_format包括需要转换为yyyy-mm-dd格式的列名称字符串.

在下面的代码,我首先通过加载日期列的CSV作为StringType schema,然后我检查是否date_format是不空的,也就是说有需要被转换为列DateString,然后使用浇铸每一列unix_timestampto_date.但是,在中csv_df.show(),返回的行都是null.

def read_csv(csv_source:String, delimiter:String, is_first_line_header:Boolean, 
    schema:StructType, date_format:List[String]): DataFrame = {
    println("|||| Reading CSV Input ||||")

    var csv_df = sqlContext.read
        .format("com.databricks.spark.csv")
        .schema(schema)
        .option("header", is_first_line_header)
        .option("delimiter", delimiter)
        .load(csv_source)
    println("|||| Successfully read CSV. Number of rows -> " + csv_df.count() + " ||||")
    if(date_format.length > 0) {
        for (i <- 0 until date_format.length) {
            csv_df = csv_df.select(to_date(unix_timestamp(
                csv_df(date_format(i)), "yyyy-­MM-­dd").cast("timestamp")))
            csv_df.show()
        }
    }
    csv_df
}
Run Code Online (Sandbox Code Playgroud)

返回前20行:

+-------------------------------------------------------------------------+
|to_date(CAST(unix_timestamp(prom_price_date, YYYY-­MM-­DD) AS TIMESTAMP))|
+-------------------------------------------------------------------------+
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
|                                                                     null|
+-------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

为什么我得到了所有null

小智 10

要转换yyyyMMddyyyy-MM-dd您可以:

spark.sql("""SELECT DATE_FORMAT(
  CAST(UNIX_TIMESTAMP('20161025', 'yyyyMMdd') AS TIMESTAMP), 'yyyy-MM-dd'
)""")
Run Code Online (Sandbox Code Playgroud)

功能:

date_format(unix_timestamp(col, "yyyyMMdd").cast("timestamp"), "yyyy-MM-dd")
Run Code Online (Sandbox Code Playgroud)