zoukankan      html  css  js  c++  java
  • 嵌入式 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中执行成功。

  • 相关阅读:
    java通过dom读写xml文件
    SWT
    Eclipse RCP学习资料
    Integration Guide
    一个Linux命令,黑客帝国
    GET和POST两种基本请求方法的区别
    贫血模式-AnemicDomainModel
    Synchronized锁在Spring事务管理下,为啥还线程不安全?
    redis 集群配置、启动
    redis cluster集群
  • 原文地址:https://www.cnblogs.com/mustone/p/5664373.html
Copyright © 2011-2022 走看看