天天看點

Hive on Spark和Spark sql on Hive,你能分的清楚麼

摘要:結構上Hive On Spark和SparkSQL都是一個翻譯層,把一個SQL翻譯成分布式可執行的Spark程式。

本文分享自華為雲社群《Hive on Spark和Spark sql on Hive有啥差別?》,作者:dayu_dls 。

結構上Hive On Spark和SparkSQL都是一個翻譯層,把一個SQL翻譯成分布式可執行的Spark程式。Hive和SparkSQL都不負責計算。Hive的預設執行引擎是mr,還可以運作在Spark和Tez。Spark可以連接配接多種資料源,然後使用SparkSQL來執行分布式計算。

Hive On Spark 配置

(1)首先安裝包要選擇對,否則就沒有開始了。

Hive版本:apache-hive-2.1.1-bin.tar

spark版本:spark-1.6.3-bin-hadoop2.4-without-hive(不需要把Hive編譯進去)

(2)假設你已經安裝好Hive(中繼資料為Derby)和spark,那麼預設Hive走mr,需要修改以下配置讓Hive走spark

<property>
    <name>hive.execution.engine</name>
    <value>spark</value>
</property>      

(3)配置環境變量及運作時參數

在hive-site.xml中配置SPARK_HOME;

在hive-site.xml或者或者spark-default.conf或者spark-env.conf配置spark運作時參數,也可以在Hive運作環境中設定臨時參數:

set spark.master=<Spark Master URL>
set spark.eventLog.enabled=true;
set spark.eventLog.dir=<Spark event log folder (must exist)>
set spark.executor.memory=512m;            
set spark.serializer=org.apache.spark.serializer.KryoSerializer;      

将編譯好的Spark安裝包中lib目錄下的spark-assembly-*.jar包添加至HIVE_HOME/lib中

(4)啟動Hive

/opt/hive/bin/hive --service metastore      

(5)啟動Hive指令行視窗

beeline -u jdbc:hive2://localhost:10000  或者  /opt/hive/bin/hive      

(6)開啟你的Hive on spark之旅

0: jdbc:hive2://localhost:10000> create table test (f1 string,f2 string) stored as orc;

No rows affected (2.018 seconds)

0: jdbc:hive2://localhost:10000> insert into test values(1,2);      

Spark sql on Hive

(1)擷取包

spark版本:spark-1.6.3-bin-hadoop2.4(需要把Hive編譯進去)

(2)在$SPARK_HOME/conf目錄建立hive-site.xml檔案,内容如下:

<configuration>  
<property>  
    <name>hive.metastore.uris</name>  
    <value>thrift://master1:9083</value>  
    <description>Thrift URI for the remote metastore. Used by metastore client to connect to remote metastore.</description>  
  </property>  
</configuration>      

(3)如果你使用的中繼資料庫是mysql,那麼請把mysql驅動放到$SPARK_HOME/lib下,否則跳過。

(4)啟動Hive中繼資料服務,待spark運作時通路。

(5)執行指令

./bin/spark-shell --master spark://master:7077
scala> val hc = new org.apache.spark.sql.hive.HiveContext(sc);
scala> hc.sql("show tables").collect.foreach(println)
[sougou,false]
[t1,false]      

Sparkthriftserver啟用

spark提供了spark-sql指令可以直接操作hive或impala,可以啟用sparkthriftserver服務,然後利用beeline遠端連接配接spark,利用spark sql。sparksql的誕生其實就是為了代替hsql。Sparksql的中繼資料也是使用hive的metastore進行管理,是以需要配置hive.metastore.uris參數。

這裡說下sparkthriftserver和hivethriftserver的差別,二者的端口一定要區分:

hivethriftserver:hive服務端的服務,遠端通過jdbc或者beeline連接配接,使用hsql操作hive。

sparkthriftserver:spark的服務,遠端通過jdbc或者beeline連接配接spark,使用spark sql操作hive。

(1)在$SPARK_HOME/conf目錄建立hive-site.xml檔案,内容如下:

<configuration>  
<property>  
    <name>hive.metastore.uris</name>  
    <value>thrift://master1:9083</value>  
    <description>Thrift URI for the remote metastore. Used by metastore client to connect to remote metastore.</description>  
  </property>  
<!--Thrift JDBC/ODBC server-->
   <property>
       <name>hive.server2.thrift.min.worker.threads</name>
       <value>5</value>
   </property>
   <property>
       <name>hive.server2.thrift.max.worker.threads</name>
       <value>500</value>
   </property>
   <property>
       <name>hive.server2.thrift.port</name>
       <value>10001</value>
   </property>
   <property>
       <name>hive.server2.thrift.bind.host</name>
       <value>master</value>
   </property>
</configuration>        

(2)啟動sparkthriftserver

./start-thriftserver.sh --hiveconf hive.server2.thrift.port=10000 --master yarn --driver-class-path /data/spark-2.2.0-bin-hadoop2.7/jars/mysql-connector-java-5.1.43-bin.jar --executor-memory 5g --total-executor-cores 5      
啟動sparkthriftserver後,背景預設會執行spark-sql指令,實際上是用spark-submit向yarn送出一個任務。這樣就會在yarn的8088頁面工作列中起一個常駐任務,用來執行spark sql。      

(3)連接配接spark

./beeline -u jdbc:hive2://172.168.108.6:10001 -n root      
(4)這裡的sql可以在8088頁面看到執行過程。      

點選關注,第一時間了解華為雲新鮮技術~