Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache spark 在where子句上的时间戳之间触发SQL?_Apache Spark_Apache Spark Sql_Spark Dataframe - Fatal编程技术网

Apache spark 在where子句上的时间戳之间触发SQL?

Apache spark 在where子句上的时间戳之间触发SQL?,apache-spark,apache-spark-sql,spark-dataframe,Apache Spark,Apache Spark Sql,Spark Dataframe,我试图使用DataFrameAPI返回两个时间戳之间的行 示例代码为: val df = Seq( ("red", "2016-11-29 07:10:10.234"), ("green", "2016-11-29 07:10:10.234"), ("blue", "2016-11-29 07:10:10.234")).toDF("color", "date") df.where(unix_timestamp($"date", "yyyy-MM-dd HH:mm:s

我试图使用DataFrameAPI返回两个时间戳之间的行

示例代码为:

val df = Seq(
    ("red", "2016-11-29 07:10:10.234"),
    ("green", "2016-11-29 07:10:10.234"),
    ("blue", "2016-11-29 07:10:10.234")).toDF("color", "date")

  df.where(unix_timestamp($"date", "yyyy-MM-dd HH:mm:ss.S").cast("timestamp").between(LocalDateTime.now(), LocalDateTime.now().minusHours(1))).show()
但它抛出了不受支持的文本类型类java.time.LocalDateTime错误

Exception in thread "main" java.lang.RuntimeException: Unsupported literal type class java.time.LocalDateTime 2016-11-29T07:32:12.084
    at org.apache.spark.sql.catalyst.expressions.Literal$.apply(literals.scala:57)
    at org.apache.spark.sql.functions$.lit(functions.scala:101)
    at org.apache.spark.sql.Column.$greater$eq(Column.scala:438)
    at org.apache.spark.sql.Column.between(Column.scala:542)
    at com.sankar.SparkSQLTimestampDifference$.delayedEndpoint$com$sankar$SparkSQLTimestampDifference$1(SparkSQLTimestampDifference.scala:23)
    at com.sankar.SparkSQLTimestampDifference$delayedInit$body.apply(SparkSQLTimestampDifference.scala:7)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.App$class.main(App.scala:76)
    at com.sankar.SparkSQLTimestampDifference$.main(SparkSQLTimestampDifference.scala:7)
    at com.sankar.SparkSQLTimestampDifference.main(SparkSQLTimestampDifference.scala)

在where子句中使用
Timestamp
时,需要将
LocalDateTime
转换为
Timestamp
。还要注意,
between
的第一个参数是
lowerBound
,因此在您的例子中,
LocalDateTime.now().minusHours(1)
应该在
LocalDateTime.now()之前。然后你可以做:

import java.time.LocalDateTime
import java.sql.Timestamp

df.where(
     unix_timestamp($"date", "yyyy-MM-dd HH:mm:ss.S")
       .cast("timestamp")
       .between(
          Timestamp.valueOf(LocalDateTime.now().minusHours(1)),
          Timestamp.valueOf(LocalDateTime.now())
       ))
  .show()
你会像这样被过滤掉

+-----+--------------------+
|color|                date|
+-----+--------------------+
|  red|2016-11-29 10:58:...|
+-----+--------------------+

在where子句中使用
Timestamp
时,需要将
LocalDateTime
转换为
Timestamp
。还要注意,
between
的第一个参数是
lowerBound
,因此在您的例子中,
LocalDateTime.now().minusHours(1)
应该在
LocalDateTime.now()之前。然后你可以做:

import java.time.LocalDateTime
import java.sql.Timestamp

df.where(
     unix_timestamp($"date", "yyyy-MM-dd HH:mm:ss.S")
       .cast("timestamp")
       .between(
          Timestamp.valueOf(LocalDateTime.now().minusHours(1)),
          Timestamp.valueOf(LocalDateTime.now())
       ))
  .show()
你会像这样被过滤掉

+-----+--------------------+
|color|                date|
+-----+--------------------+
|  red|2016-11-29 10:58:...|
+-----+--------------------+
Protip:使用
.show(truncate=false)
查看整个数据。是否确实需要
cast(“时间戳”)
unix\u timestamp
是否尚未返回
timestamp
?Protip:使用
.show(truncate=false)
查看整个数据。是否确实需要
cast(“timestamp”)
unix\u时间戳
是否尚未返回
时间戳