Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

scala - Spark not working with pureconfig

I'm trying to use pureConfig and configFactory for my spark application configuration. here is my code:

import pureconfig.{loadConfigOrThrow}
object Source{
  def apply(keyName: String, configArguments: Config): Source = {
    keyName.toLowerCase match {
      case "mysql" =>
          val properties = loadConfigOrThrow[DBConnectionProperties](configArguments)
          new MysqlSource(None, properties)
      case "files" =>
        val properties = loadConfigOrThrow[FilesSourceProperties](configArguments)
        new Files(properties)
      case _ => throw new NoSuchElementException(s"Unknown Source ${keyName.toLowerCase}")
    }

  }
}

import Source
val config = ConfigFactory.parseString(result.mkString("
"))
    val source = Source("mysql",config.getConfig("source.mysql"))

when I run it from the IDE (intelliJ) or directly from java (i.e java jar...) it works fine.

But when I run it with spark-submit it fails with the following error:

Exception in thread "main" java.lang.NoSuchMethodError: shapeless.Witness$.mkWitness(Ljava/lang/Object;)Lshapeless/Witness;

From a quick search I found a similar similar to this question. which suggest the reason for this is due to the fact both spark and pureConfig depends on Shapeless but with different versions,

I tried to shade it as suggested in the answer

assemblyShadeRules in assembly := Seq(
  ShadeRule.rename("shapeless.**" -> "shadeshapless.@1")
    .inLibrary("com.github.pureconfig" %% "pureconfig" % "0.7.0").inProject
)

but it didn't work as well can it be from a different reason? any idea what may work?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You also have to shade shapeless inside its own JAR, in addition to pureconfig:

assemblyShadeRules in assembly := Seq(
  ShadeRule.rename("shapeless.**" -> "shadeshapless.@1")
    .inLibrary("com.chuusai" % "shapeless_2.11" % "2.3.2")
    .inLibrary("com.github.pureconfig" %% "pureconfig" % "0.7.0")
    .inProject
)

Make sure to add the correct shapeless version.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...