嵌入式 ThriftServer in Spark


我们知道在Spark中可以通过start-thriftServer.sh 来启动ThriftServer,之后并可以通过beeline或者JDBC来连接并执行Spark SQL。在一般的Spark应用中,通常并不希望另外起一个服务进程,自然就要问:可以在Spark dirver program里启一个嵌入式的ThriftServer吗? 

答案是Yes。要启动ThriftServer,首先需要HiveContext,并且需要在Spark中已经configure好了Hive。通过启动HiveContext,可以利用 DataFrame 的saveAsTable方法将dataframe save 成 Hive table,达到持久化效果。下面是代码示例:

import org.apache.spark.sql.hive.HiveContext
import  org.apache.spark.sql.hive.thriftserver._
 
// start the Thrift Server with existing sqlContext casting to HiveContext
HiveThriftServer2.startWithContext(sqlContext.asInstanceOf[HiveContext])
 
// wisdom_lu_country has two columns: id and desc 
case class lu_country(id:Short,desc:String)
 
// load the file as RDD, split each line to id and desc, and convert it to DataFrame
val countryDF = sc.textFile("/FB_100/wisdom_lu_country.csv").map(_.split('^')).map(p=>lu_country(p(0).toShort,p(1))).toDF()

// save as Hive table
countryDF.write.saveAsTable("wisdom_lu_country")

上述代码在spark-shell中执行成功。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM