(spark)对象{name}不是org.apache.spark.ml包的成员

f5emj3cl  于 2021-05-27  发布在  Spark
关注(0)|答案(2)|浏览(640)

我尝试在ApacheSpark上使用scala运行自包含应用程序,基于以下示例:http://spark.apache.org/docs/latest/ml-pipeline.html
以下是我的完整代码:

import org.apache.spark.ml.classification.LogisticRegression
import org.apache.spark.ml.linalg.{Vector, Vectors}
import org.apache.spark.ml.param.ParamMap
import org.apache.spark.sql.Row

object mllibexample1 {
  def main(args: Array[String]) {
    val spark = SparkSession
      .builder()
      .master("local[*]")
      .appName("logistic regression example 1")
      .getOrCreate()

    val training = spark.createDataFrame(Seq(
      (1.0, Vectors.dense(0.0, 1.1, 0.1)),
      (0.0, Vectors.dense(2.0, 1.0, -1.0)),
      (0.0, Vectors.dense(2.0, 1.3, 1.0)),
      (1.0, Vectors.dense(0.0, 1.2, -0.5))
    )).toDF("label", "features")

    val lr = new LogisticRegression()

    println("LogisticRegression parameters:\n" + lr.explainParams() + "\n")

    lr.setMaxIter(100)
      .setRegParam(0.01)

    val model1 = lr.fit(training)

    println("Model 1 was fit using parameters: " + model1.parent.extractParamMap)
  }
}

build.sbt中的依赖项:

name := "example"
version := "1.0.0"
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
    "org.apache.spark" %% "spark-core" % "2.0.1",
    "org.apache.spark" %% "spark-sql" % "2.0.1",
    "org.apache.spark" %% "spark-mllib-local" % "2.0.1",
    "com.github.fommil.netlib" % "all" % "1.1.2"
  )

但是,在sbt shell中运行程序后,出现以下错误:

[info] Compiling 1 Scala source to /dataplatform/example/target/scala-2.11/classes...
[error] /dataplatform/example/src/main/scala/mllibexample1.scala:1: object classification is not a member of package org.apache.spark.ml
[error] import org.apache.spark.ml.classification.LogisticRegression
[error]                            ^
[error] /dataplatform/example/src/main/scala/mllibexample1.scala:3: object param is not a member of package org.apache.spark.ml
[error] import org.apache.spark.ml.param.ParamMap
[error]                            ^
[error] /dataplatform/example/src/main/scala/mllibexample1.scala:8: not found: value SparkSession
[error]     val spark = SparkSession
[error]                 ^
[error] /dataplatform/example/src/main/scala/mllibexample1.scala:22: not found: type LogisticRegression
[error]     val lr = new LogisticRegression()

我可以在spark交互式shell中成功运行此代码。我在*.sbt文件中遗漏了什么吗?
谢谢,巴渝

llmtgqce

llmtgqce1#

我有同样的问题,我有一个maven scala项目。
我使用了下面的maven依赖项。添加这个maven依赖项之后,问题就解决了。

<dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-mllib_2.11</artifactId>
            <version>2.0.2</version>
        </dependency
v2g6jxz6

v2g6jxz62#

您错过了一个mllib依赖项:

"org.apache.spark" %% "spark-mllib" % "2.0.1"

本地的还不够。

相关问题