(1)registerTempTable函數(shù)是SQLContext類中的,所以我們可以將people轉(zhuǎn)換成SchemaRDD,如下:
02 * User: 過(guò)往記憶03 * Date: 14-12-1604 * Time: 下午10:1605 * bolg: http://www.iteblog.com06 * 本文地址:http://www.iteblog.com/archives/122407 * 過(guò)往記憶博客,專注于hadoop、hive、spark、shark、flume的技術(shù)博客,大量的干貨08 * 過(guò)往記憶博客微信公共帳號(hào):iteblog_hadoop09 */10scala> val peopleSchema =sqlContext.createSchemaRDD(people)11peopleSchema:org.apache.spark.sql.SchemaRDD =12SchemaRDD[29] at RDD at SchemaRDD.scala:10313==Query Plan ==14==Physical Plan ==15ExistingRdd [name#4,age#5], MapPartitionsRDD[28] at16 mapPartitions at basicOperators.scala:21717 18scala> peopleSchema.registerTempTable(\”people\”)19warning:there were 1deprecation warning(s); re-run with-deprecation fordetails
這么調(diào)用就可以將people轉(zhuǎn)成SchemaRDD。
(2)、上面的方法是通過(guò)顯示地調(diào)用sqlContext.createSchemaRDD將普通的RDD轉(zhuǎn)成SchemaRDD。其實(shí)我們還可以通過(guò)Scala的隱式語(yǔ)法來(lái)進(jìn)行轉(zhuǎn)換。我們先來(lái)看看createSchemaRDD函數(shù)的定義
1/**2* Creates a SchemaRDD from an RDD of case classes.3*4* @group userf5*/6implicitdefcreateSchemaRDD[A <:Product:TypeTag](rdd:RDD[A]) ={7 SparkPlan.currentContext.set(self)8 newSchemaRDD(this, SparkLogicalPlan(ExistingRdd.fromProductRdd(rdd))(self))9}
在定義createSchemaRDD的時(shí)候用到了implicit 關(guān)鍵字,所以我們?cè)谑褂玫臅r(shí)候可以通過(guò)下面語(yǔ)句使用
1scala> import sqlContext.createSchemaRDD2import sqlContext.createSchemaRDD3 4scala> people.registerAsTable(\”people\”)5warning:there were 1deprecation warning(s); re-run with-deprecation fordetails
這樣就隱身地將people轉(zhuǎn)換成SchemaRDD了。這是因?yàn)镾park可以隱式地將包含case class的RDD轉(zhuǎn)換成SchemaRDD。
看完上述內(nèi)容,你們對(duì)怎么實(shí)現(xiàn)Spark SchemaRDD隱式轉(zhuǎn)換有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注資訊頻道,感謝大家的支持。
更多關(guān)于云服務(wù)器,域名注冊(cè),虛擬主機(jī)的問(wèn)題,請(qǐng)?jiān)L問(wèn)西部數(shù)碼官網(wǎng):www.ps-sw.cn