Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

value toDF is not a member of Seq[(Int,String)]

I am trying to execute the following code but getting this error: value toDF is not a member of Seq[(Int,String)].

I have the case class outside main and I have imported implicits too. But still I am getting this error. Can someone help me to resolve this ? I am using Spark 2.11-2.1.0 and Scala 2.11.8

  import org.apache.spark.sql._
  import org.apache.spark.ml.clustering._
  import org.apache.spark.ml.feature.VectorAssembler
  import org.apache.spark._


  final case class Email(id: Int, text: String)

  object SampleKMeans {

     def main(args: Array[String]) = {

     val spark = SparkSession.builder.appName("SampleKMeans") 
                 .master("yarn")
                 .getOrCreate()

     import spark.implicits._

     val emails = Seq(
             "This is an email from...",
             "SPAM SPAM spam",
             "Hello, We'd like to offer you") 
             .zipWithIndex.map(_.swap).toDF("id", "text").as[Email]

    }
  }
like image 504
prasmgr Avatar asked Apr 18 '18 12:04

prasmgr


1 Answers

You already have a SparkSession you can just import the spark.implicits._ will work in your case

val spark = SparkSession.builder.appName("SampleKMeans") 
             .master("local[*]")
             .getOrCreate()
import spark.implicits._

Now toDF method works as expected.

If the error still exists, You need to check the version of spark and scala libraries that you are using.

Hope this helps!

like image 67
koiralo Avatar answered Sep 30 '22 21:09

koiralo