DWQA QuestionsCategory: Artificial IntelligenceNoSuchMethodError: scala.Product.$init$(Lscala/Product;)V
shuai7boy asked 1 year ago

Recently, when learning big data and creating datasets with Scala, the following exception occurred in the running code:

Exception in thread "main" java.lang.NoSuchMethodError: scala.Product.$init$(Lscala/Product;)V
    at com.shuai7boy.myscalacode.Person.<init>(Test.scala:17)
    at com.shuai7boy.myscalacode.Test$.main(Test.scala:10)
    at com.shuai7boy.myscalacode.Test.main(Test.scala)

My program code is as follows:

object Test {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().appName("spark SQL Demo").master("local").getOrCreate()
    import spark.implicits._
    Val caseclassds = SEQ (person ("Zhang San", 23)). Tods ()

case class Person(name: String, age: Int)

As expected, the following results should be printed

// +----+---+
// |name|age|
// +----+---+
//Zhang San 23|
// +----+---+

However, it reported a mistake. Then I searched the Internet for solutions, all of which were Scala version problems. However, I read my version and all of them were correct.
How can I solve this problem? Who can help me ~ thank you!

1 Answers
shuai7boy answered 1 year ago

The problem has been solved. After carefully comparing the next project, I found that there are two references: one is spark referenced by Maven and the other is a local package referenced manually. Then, an error is reported. Remove the local treasure and change it to Maven reference.
Summary: to be clear, do not use the local package and Maven package at the same time, this is a pit.