Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Spark) object {name} is not a member of package org.apache.spark.ml

I'm trying to run self-contained application using scala on apache spark based on example here: http://spark.apache.org/docs/latest/ml-pipeline.html

Here's my complete code:

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)
  }
}

Dependencies in 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"
  )

However after running the program in sbt shell, I got the following error:

[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()

I can successfully run this code in spark interactive shell. Did I miss something in *.sbt file ?

Thanks, Bayu

like image 775
Yusata Avatar asked Oct 27 '16 10:10

Yusata


2 Answers

I had the same issue and I am having a Maven Scala project.

I used the below Maven dependency. After adding this maven dependency, the issue was resolved.

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-mllib_2.11</artifactId>
            <version>2.0.2</version>
        </dependency
like image 86
Rajkumar S Avatar answered Oct 13 '22 01:10

Rajkumar S


You missed a MLlib dependency:

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

Local is not enough.

like image 27
user6022341 Avatar answered Oct 12 '22 23:10

user6022341