Search code examples
scalaapache-sparkshapelesspureconfig

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("\n"))
    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


Solution

  • 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.