2017-08-23 78 views
0

我想使用ShapeLogic Sc​​ala相結合Spark。我正在使用Scala 2.11.8Spark 2.1.1ShapeLogic Sc​​ala 0.9.0。 我成功導入類來管理Spark圖像。此外,我已成功編譯和打包(通過使用SBT)以下應用程序,以便將其提交到羣集。SparkSubmit異常(NoClassDefFoundError),即使SBT編譯併成功打包

下面的應用程序只是打開的圖像,並將其寫入到一個文件夾:

// imageTest.scala 
import org.apache.spark.sql.SparkSession 
import org.shapelogic.sc.io.LoadImage 
import org.shapelogic.sc.image.BufferImage 
import org.shapelogic.sc.io.BufferedImageConverter 

object imageTestObj { 
    def main(args: Array[String]) { 
     // Create a Scala Spark Session 
     val spark = SparkSession.builder().appName("imageTest").master("local").getOrCreate(); 

     val inPathStr = "/home/vitrion/IdeaProjects/imageTest"; 
     val outPathStr = "/home/vitrion/IdeaProjects/imageTest/output"; 
     val empty = new BufferImage[Byte](0, 0, 0, Array()); 
     var a = Array.fill(3)(empty); 
     for (i <- 0 to 3) { 
      val imagePath = inPathStr + "IMG_" + "%01d".format(i + 1); 
      a(i) = LoadImage.loadBufferImage(inPathStr); 
     } 
     val sc = spark.sparkContext; 
     val imgRDD = sc.parallelize(a); 
     imgRDD.map { outBufferImage => 
      val imageOpt = BufferedImageConverter.bufferImage2AwtBufferedImage(outBufferImage) 
      imageOpt match { 
       case Some(bufferedImage) => { 
        LoadImage.saveAWTBufferedImage(bufferedImage, "png", outPathStr) 
        println("Saved " + outPathStr) 
       } 
       case None => { 
        println("Could not convert image") 
       } 
      } 
     } 
    } 
} 

這是我的SBT文件

name := "imageTest" 

version := "0.1" 

scalaVersion := "2.11.8" 

libraryDependencies ++= Seq(
    "org.apache.spark" % "spark-core_2.11" % "2.1.1" % "provided", 
    "org.apache.spark" % "spark-sql_2.11" % "2.1.1" % "provided", 
    "org.shapelogicscala" %% "shapelogic" % "0.9.0" % "provided" 
) 

然而,出現以下錯誤。當執行 SBT命令,好像不包括在應用程序JAR的ShapeLogic斯卡拉依賴性:

[[email protected] scala-2.11]$ pwd 
/home/vitrion/IdeaProjects/imageTest/target/scala-2.11 
[[email protected] scala-2.11]$ ls 
classes imagetest_2.11-0.1.jar resolution-cache 
[[email protected] scala-2.11]$ spark-submit --class imageTestObj imagetest_2.11-0.1.jar 
Exception in thread "main" java.lang.NoClassDefFoundError: org/shapelogic/sc/image/BufferImage 
    at imageTestObj.main(imageTest.scala) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:743) 
    at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:187) 
    at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:212) 
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:126) 
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala) 
Caused by: java.lang.ClassNotFoundException: org.shapelogic.sc.image.BufferImage 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 10 more 

我希望有人能幫助我解決呢? 非常感謝您

回答

1

這種錯誤說明了一切:

Caused by: java.lang.ClassNotFoundException: org.shapelogic.sc.image.BufferImage 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 

添加這個缺少的依賴(ShapeLogic類)------> org.shapelogic.sc.image.BufferImage,應該可以解決這個問題。如果你錯過了這個依賴關係,Maven或者SBT都應該給出相同的錯誤!

由於您正在使用集羣模式,因此您可以在spark-submit上使用--jars直接添加依賴關係,請按照此post瞭解更多詳細信息。

這些線程可以幫助您: Link1 Link2

+0

非常感謝您的回覆。我知道打包應用程序時,BufferImage類不包含在JAR文件中。但是,我怎麼能包括它?正如我之前所說的,SBT編譯和打包成功 – Vitrion

+0

更新了答案! – Jordon

+0

@Vitrion你在使用集羣模式還是客戶端模式?看到這個https://stackoverflow.com/questions/34272426/how-to-give-dependent-jars-to-spark-submit-in-cluster-mode?rq=1可能會幫助你 – Jordon

0

你依賴於SBT文件中列出默認情況下不會在提交給火花,所以對SBT你必須使用一個插件來構建一個超級/脂肪罐子,其中將包括shapelogicscala您的罐子被列入類。您可以在SO,How to build an Uber JAR (Fat JAR) using SBT within IntelliJ IDEA?上使用此鏈接查看如何使用sbt進行管理。

+0

確定。讓我試試看。非常感謝你 – Vitrion

+0

好的。我使用了相同的方法,但與Maven一起工作。 – dumitru