为什么spark-xml失败,NoSuchMethodError与Spark 2.0.0依赖?

时间:2017-10-11 12:47:16

标签: scala apache-spark apache-spark-xml

您好我是Scala和Intellij的菜鸟,我只是想在Scala上做这个:

import org.apache.spark
import org.apache.spark.sql.SQLContext
import com.databricks.spark.xml.XmlReader


object SparkSample {
  def main(args: Array[String]): Unit = {
    val conf = new spark.SparkConf()
    conf.setAppName("Datasets Test")
    conf.setMaster("local[2]")
    val sc = new spark.SparkContext(conf)

    val sqlContext = new SQLContext(sc)
    val df = sqlContext.read
      .format("com.databricks.spark.xml")
      .option("rowTag", "shop")
      .load("shops.xml") /* NoSuchMethod error here */

    val selectedData = df.select("author", "_id")
  df.show
}

基本上我正在尝试将XML转换为spark数据帧 我在'.load(“shops.xml”)中收到NoSuchMethod错误  以下是SBT

version := "0.1"

scalaVersion := "2.11.3"
val sparkVersion = "2.0.0" 
val sparkXMLVersion = "0.3.3"

libraryDependencies ++= Seq(
  "org.apache.spark"      %%  "spark-core"      %   sparkVersion  exclude("jline", "2.12"),
  "org.apache.spark"      %% "spark-sql"        % sparkVersion excludeAll(ExclusionRule(organization = "jline"),ExclusionRule("name","2.12")),
   "com.databricks"        %% "spark-xml"        % sparkXMLVersion,
)

以下是追踪:

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.spark.sql.types.DecimalType$.Unlimited()Lorg/apache/spark/sql/types/DecimalType;
at com.databricks.spark.xml.util.InferSchema$.<init>(InferSchema.scala:50)
at com.databricks.spark.xml.util.InferSchema$.<clinit>(InferSchema.scala)
at com.databricks.spark.xml.XmlRelation$$anonfun$1.apply(XmlRelation.scala:46)
at com.databricks.spark.xml.XmlRelation$$anonfun$1.apply(XmlRelation.scala:46)
at scala.Option.getOrElse(Option.scala:120)
at com.databricks.spark.xml.XmlRelation.<init>(XmlRelation.scala:45)
at com.databricks.spark.xml.DefaultSource.createRelation(DefaultSource.scala:66)
at com.databricks.spark.xml.DefaultSource.createRelation(DefaultSource.scala:44)
at org.apache.spark.sql.execution.datasources.DataSource.resolveRelation(DataSource.scala:315)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:149)
at org.apache.spark.sql.DataFrameReader.load(DataFrameReader.scala:132)

有人可以指出错误吗?对我来说似乎是依赖问题。 spark-core似乎工作正常,但不是spark-sql 之前我有过scala 2.12,但因为spark-core没有解决而改为2.11

1 个答案:

答案 0 :(得分:2)

tl; dr 我认为这是Scala版本不匹配问题。使用spark-xml 0.4.1

引用spark-xml的Requirements(突出显示我的):

  

此库需要Spark 2.0+ for 0.4.x。

     

对于适用于 Spark 1.x 的版本,请检查 branch-0.3

对我说,spark-xml 0.3.3适用于Spark 1.x(不是你要求的Spark 2.0.0)。