【问题标题】:Is it possible to do a date-diff on a timestamp column with the current timestamp in Apache Spark?是否可以在 Apache Spark 中使用当前时间戳对时间戳列进行日期差异?
【发布时间】:2015-07-09 06:53:55
【问题描述】:

我正在尝试加载包含两个时间戳列的制表符分隔文件并生成一个计算列,该列是其中一个列与当前时间戳之间的差异(以天为单位)。我在 RDD 上应用了 registerTempTable() 方法将其转换为 SchemaRDD。之后我几乎碰壁了,因为所有后续操作都依赖于这个 datediff 计算字段。

这是我到目前为止所做的。感谢您的帮助!

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._


val conf = new SparkConf().setMaster("local[2]").setAppName("CookieSummary")

val sc = new SparkContext(conf)
val sqlContext = new org.apache.spark.sql.SQLContext(sc)

import sqlContext.createSchemaRDD

case class CookieDates(CLPartnerSyncCreateDT: String, CookieSyncRequestDT: String)
val cookies = sc.textFile("/Users/shubhro/Documents/dataFiles/clean/worker1.01012015.1420081201_sub.tsv").map(_.split("\t")).map(p => CookieDates(p(0), p(1)))

cookies.registerTempTable("cookies")

val allCookies = sqlContext.sql("SELECT CAST(CLPartnerSyncCreateDT AS TIMESTAMP),CAST(CookieSyncRequestDT AS TIMESTAMP)  FROM cookies")

allCookies.collect().foreach(println)

【问题讨论】:

    标签: scala apache-spark


    【解决方案1】:

    在 Spark 1.5.0 中引入了一个内置函数:

    https://issues.apache.org/jira/browse/SPARK-8185

    【讨论】:

      【解决方案2】:

      您使用的是哪个版本的 spark?我注意到你使用了SchemaRDD,这个概念在 Spark 1.3.0 中已被 DataFrame 取代。

      您需要在这里定义一个User Defined Function 并在sql(...) 中使用它

      在 Spark 1.2.0 中:

      val x = new TimeStamp(...) // The base time you want to diff
      registerFunction("dateDiff", (arg: TimeStamp) => (arg - x))
      sql("select dateDiff(col_name_here) from cookies")
      

      Spark 1.3.0 之后:见dataFrame notion on UDF usage

      【讨论】:

        猜你喜欢
        • 2018-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-05
        • 1970-01-01
        • 2012-11-06
        • 1970-01-01
        相关资源
        最近更新 更多