首页 > 其他 > 详细

嵌入式 ThriftServer in Spark

时间:2016-07-12 19:04:05      阅读:280      评论:0      收藏:0      [点我收藏+]

我们知道在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中执行成功。

 

嵌入式 ThriftServer in Spark

原文:http://www.cnblogs.com/mustone/p/5664373.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!