Apache spark Spark-30分钟通用窗口

Apache spark Spark-30分钟通用窗口,apache-spark,apache-spark-sql,Apache Spark,Apache Spark Sql,我目前正在编写一个spark脚本,每30分钟查看一次,并确定30分钟滚动周期内一列的平均值 我的时间戳格式为:MM/dd/yyyy HH:MM:ss AM/PM。基本上,我想做的是每30分钟看一次,不包括日期。(即下午1:02至1:32之间的所有天数的平均乘客数) 我当前的脚本将获取我的时间戳,将其转换为unix时间戳,并将其存储为新列。然后,查看当前时间戳,它将减去900秒,再加上900秒以获取前15分钟的记录和当前时间戳后15分钟的记录。这给了我30分钟的时间。当我在创建新列“timesta

我目前正在编写一个spark脚本,每30分钟查看一次,并确定30分钟滚动周期内一列的平均值

我的时间戳格式为:
MM/dd/yyyy HH:MM:ss AM/PM
。基本上,我想做的是每30分钟看一次,不包括日期。(即下午1:02至1:32之间的所有天数的平均乘客数)

我当前的脚本将获取我的时间戳,将其转换为unix时间戳,并将其存储为新列。然后,查看当前时间戳,它将减去900秒,再加上900秒以获取前15分钟的记录和当前时间戳后15分钟的记录。这给了我30分钟的时间。当我在创建新列“timestamp”时包含
MM/dd/yyyy
时,这一点就起作用了:

val taxiSub = spark.read.format("csv").option("header", true).option("inferSchema", true).load("/user/zeppelin/taxi/taxi_subset.csv")
taxiSub.createOrReplaceTempView("taxiSub")
val stamp = taxiSub.withColumn("timestamp", unix_timestamp($"tpep_pickup_datetime", "MM/dd/yyyy HH:mm"))
import org.apache.spark.sql.expressions._
val windowSpec = Window.partitionBy("VendorID").orderBy("timestamp").rangeBetween(-900,900)
val answer = stamp.withColumn("AvgPassenger", avg(stamp("passenger_count")).over(windowSpec))
answer.select("VendorID", "tpep_pickup_datetime", "timestamp", "passenger_count", "AvgPassenger")
answer.createOrReplaceTempView("answerTable")
spark.sqlContext.sql("SELECT timestamp, AvgPassenger FROM answerTable ORDER BY AvgPassenger DESC limit 10").show()
然而,这给了我具体的日期,包括在我的范围,而不是一般的时间段上面提到。当我试图从时间戳生成中删除
MM/dd/yyyy
时,我的所有时间戳值都变为空。此外,我如何解释时间戳的AM/PM部分


如有任何想法,将不胜感激

我们可以使用unix\u时间戳(“HH:mm”,“HH:mm”)获得通用的历元时间值,然后在我们的
orderBy
子句中使用该值

示例:

//import org.apache.spark.sql.expressions._

//sample data
//+--------+---------+---------------+--------------------+
//|VendorID|timestamp|passenger_count|tpep_pickup_datetime|
//+--------+---------+---------------+--------------------+
//|       1|    66180|              3|    12/12/2019 12:23|
//|       1|    66780|              2|    12/13/2018 12:33|
//|       2|    66180|             12|    12/13/2019 12:23|
//|       2|    69780|             13|    12/13/2018 13:23|
//+--------+---------+---------------+--------------------+

val stamp = taxiSub.withColumn("tmp",to_timestamp(col("tpep_pickup_datetime"),"MM/dd/yyyy HH:mm")).//add new timestamp type field
withColumn("timestamp", unix_timestamp(concat_ws(":",hour(col("tmp")),minute(col("tmp"))),"HH:mm")). //extract hour,minute and convert to epoch timestamp value
drop("tmp")

//partition based on vendorid
val windowSpec = Window.partitionBy("VendorID").orderBy("timestamp").rangeBetween(-900,900)

stamp.withColumn("AvgPassenger", avg(stamp("passenger_count")).over(windowSpec)).show()

//+--------+---------+---------------+--------------------+------------+
//|VendorID|timestamp|passenger_count|tpep_pickup_datetime|AvgPassenger|
//+--------+---------+---------------+--------------------+------------+
//|       1|    66180|              3|    12/12/2019 12:23|         2.5|
//|       1|    66780|              2|    12/13/2018 12:33|         2.5|
//|       2|    66180|             12|    12/13/2019 12:23|        12.0|
//|       2|    69780|             13|    12/13/2018 13:23|        13.0|
//+--------+---------+---------------+--------------------+------------+