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中执行成功。

  • 相关阅读:
    使用推荐使用的映射器和适配器
    第一个SpringMVC的注解应用
    简化SpringMVC配置
    第一个SpringMVC程序
    ettercap
    hashcat
    二维数组实现checkbox的分组多选
    安装 slowhttptest ddos攻击软件
    转载:windows的mysql提权方式
    windows c dll的创建与调用
  • 原文地址:https://www.cnblogs.com/mustone/p/5664373.html
Copyright © 2011-2022 走看看