2015-11-14 8 views
11

अपवाद:मूल्य toDF org.apache.spark.rdd.RDD का सदस्य नहीं है

package main.scala 

import org.apache.spark.SparkContext 
import org.apache.spark.SparkContext._ 
import org.apache.spark.SparkConf 


case class Record1(k: Int, v: String) 


object RDDToDataFramesWithCaseClasses { 

    def main(args: Array[String]) { 
     val conf = new SparkConf().setAppName("Simple Spark SQL Application With RDD To DF") 

     // sc is an existing SparkContext. 
     val sc = new SparkContext(conf) 

     val sqlContext = new SQLContext(sc) 

     // this is used to implicitly convert an RDD to a DataFrame. 
     import sqlContext.implicits._ 

     // Define the schema using a case class. 
     // Note: Case classes in Scala 2.10 can support only up to 22 fields. To work around this limit,package main.scala 

और TestApp.scala

import org.apache.spark.SparkContext  
import org.apache.spark.SparkContext._ 
import org.apache.spark.SparkConf 


case class Record1(k: Int, v: String) 


object RDDToDataFramesWithCaseClasses { 
    def main(args: Array[String]) { 
     val conf = new SparkConf().setAppName("RDD To DF") 

     // sc is an existing SparkContext. 
     // you can use custom classes that implement the Product interface. 
     case class Person(name: String, age: Int) 

     // Create an RDD of Person objects and register it as a table. 
     val people = sc.textFile("resources/people.txt").map(_.split(",")).map(p => Person(p(0), p(1).trim.toInt)).toDF() 
     people.registerTempTable("people") 

     // SQL statements can be run by using the sql methods provided by sqlContext. 
     val teenagers = sqlContext.sql("SELECT name, age FROM people WHERE age >= 13 AND age <= 19") 

     // The results of SQL queries are DataFrames and support all the normal RDD operations. 
     // The columns of a row in the result can be accessed by field index: 
     teenagers.map(t => "Name: " + t(0)).collect().foreach(println) 

     // or by field name: 
     teenagers.map(t => "Name: " + t.getAs[String]("name")).collect().foreach(println) 

     // row.getValuesMap[T] retrieves multiple columns at once into a Map[String, T] 

     teenagers.map(_.getValuesMap[Any](List("name", "age"))).collect().foreach(println) 

     // Map("name" -> "Justin", "age" -> 19) 

    } 
} 
:

val people = sc.textFile("resources/people.txt").map(_.split(",")).map(p => Person(p(0), p(1).trim.toInt)).toDF() 
value toDF is not a member of org.apache.spark.rdd.RDD[Person] 

यहाँ TestApp.scala फ़ाइल है

और एसबीटी फ़ाइल

name := "SparkScalaRDBMS" 
version := "1.0" 
scalaVersion := "2.11.7" 
libraryDependencies += "org.apache.spark" %% "spark-core" % "1.5.1" 
libraryDependencies += "org.apache.spark" %% "spark-sql" % "1.5.1" 
+0

क्या आपने प्रतिकृति उर्फ ​​स्पार्क-खोल 'आयात sqlContext.implicits._' में किया था ?? – WoodChopper

+0

@WoodChopper ... हाँ मैंने किया लेकिन एक ही त्रुटि आ रही है। –

+0

@ आशीष आगरवाल कृपया [संपादित करें] (http://stackoverflow.com/posts/33704831/edit) कोड (कोड का कम से कम हिस्सा डुप्लिकेट किया गया है) और अप्रचलित सफेद जगहों को हटा दें ताकि यह प्रश्न कम से कम दूरस्थ रूप से पठनीय हो सके। – zero323

उत्तर

24

अब मुझे कारण मिला है, आपको ऑब्जेक्ट में केस क्लास को परिभाषित करना चाहिए और मुख्य कार्य से बाहर करना चाहिए। look at here

ठीक है, मैंने अंततः इस मुद्दे को ठीक किया। 2 बातें किया जा करने की जरूरत:

  1. आयात implicits: नोट यह किया जाना चाहिए कि बाद ही org.apache.spark.sql.SQLContext का एक उदाहरण बन जाता है। विधि के बाहर

    val sqlContext= new org.apache.spark.sql.SQLContext(sc)

    import sqlContext.implicits._

  2. ले जाएँ मामले वर्ग:: यह के रूप में लिखा जाना चाहिए मामले वर्ग, उपयोग के द्वारा, जिनमें से आप DataFrame की स्कीमा निर्धारित, के बाहर परिभाषित किया जाना चाहिए विधि की आवश्यकता है। आप यहाँ इसके बारे में अधिक पढ़ सकते हैं: https://issues.scala-lang.org/browse/SI-6649

+0

मेरा मुद्दा था # 2. उस समाधान के लिए धन्यवाद। – Salsero69

0
i. scala> case class Employee(id: Int, name: String, age: Int) 
defined class Employee 
scala> val sqlContext= new org.apache.spark.sql.SQLContext(sc) 
warning: there was one deprecation warning; re-run with -deprecation for details 
sqlContext: org.apache.spark.sql.SQLContext = [email protected] 

scala> import sqlContext.implicits._ 
import sqlContext.implicits._ 
scala> var empl1= empl.map(_.split(",")).map(e=>Employee(e(0).trim.toInt,e(1),e(2).trim.toInt)).toDF 
empl1: org.apache.spark.sql.DataFrame = [id: int, name: string ... 1 more field] 
scala> val allrecords = sqlContext.sql("SELECT * FROM employee") 
allrecords: org.apache.spark.sql.DataFrame = [id: int, name: string ... 1 more field] 

scala> allrecords.show(); 
+----+--------+---+ 
| id| name|age| 
+----+--------+---+ 
|1201| satish| 25| 
|1202| krishna| 28| 
|1203| amith| 39| 
|1204| javed| 23| 
|1205| prudvi| 23| 
+----+--------+---+ 
0

स्पार्क 2 में, आप SparkSession से implicits आयात करने की आवश्यकता:

val spark = SparkSession.builder().appName(appName).getOrCreate() 
import spark.implicits._ 

अधिक विकल्पों के लिए Spark documentation देखें स्पार्क सत्र बनाते समय।

संबंधित मुद्दे