如何用Spark中的nscala_time计算时差

时间:2016-12-20 08:58:20

标签: scala apache-spark nscala-time

我正在尝试计算T1和T2的时差。 我在spark-shell中的代码如下:

scala> import com.github.nscala_time.time.Imports._
import com.github.nscala_time.time.Imports._
scala> import org.joda.time.DateTime
import org.joda.time.DateTime

scala> val T1 = DateTime.parse("2014-10-12T10:32:32")
T1: org.joda.time.DateTime = 2014-10-12T10:32:32.000+08:00
scala> val T2 = DateTime.parse("2014-10-12T10:33:32")
T2: org.joda.time.DateTime = 2014-10-12T10:33:32.000+08:00
scala> val diff = (T1 to T2).millis/1000
diff: Long = 60
一切正常。 所以我用sbt提交相同的代码: enter image description here

我的依赖文件如下: enter image description here

提交代码:

spark-shell --class "YourApp" --jars /usr/local/spark/lib_third/nscala-time-master/target/scala-2.10/nscala-time_2.10-2.15.0-SNAPSHOT.jar ~/Spark_nscala2/target/scala-2.10/YourApp_2.10-1.0.jar

然而,它出错了:

Exception in thread "main" java.lang.NoSuchMethodError: com.github.nscala_time.time.Imports$.RichReadableInstant(Lorg/joda/time/ReadableInstant;)Lcom/github/nscala_time/time/RichReadableInstant;
    at YourApp$.main(YourApp.scala:15)
    at YourApp.main(YourApp.scala)

异常显示问题来自这句话:

val diff = (T1 to T2).millis/1000

对我来说没有意义,相同的代码在spark-shell中工作但在提交时失败。 有人能告诉我问题出在哪里吗?先谢谢!

2 个答案:

答案 0 :(得分:0)

尝试将第三方jar添加到--driver-class-path以及--jars

答案 1 :(得分:0)

我自己弄清楚了。我尝试了另一个版本的nscala_time jar,并相应地更改了依赖关系,问题得到解决。