spark連接hive並查詢數據

首先啓動 hadoop,mysql,hive 服務python

{hadoop}/sbin/start-all.shmysql

service mysqld start sql

hive --service metastore&oop

hive --service hiveserver2  &測試

最後啓動spark 我是以python方式啓動的sparkspa

{spark_home}/bin/pysparkcode

若是沒有報錯就是沒有什麼問題。接下來咱們運行一段代碼進行測試一下:server

>>>from pyspark.sql import HiveContext
>>>from pyspark import SparkConf,SparkContext 
>>>conf = SparkConf().setMaster("local").setAppName("My App") 
>>>sc= SparkContext(conf=conf)
>>> hiveCtx=HiveContext(sc) 
>>>hh=hiveCtx.sql("select * from test")
>>>hh.show()
>>> print hh.first()
Row(id=1)

一切ok,首先要注意下,spark連接hive的時候必定要把 hive/conf下面的hive-site.xml 拷貝到spark/conf 下面xml

在hive-site.xml 中要配置:主要指明hive的存儲位置blog

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

我在配置完成以後仍是報錯就重啓了系統,就行了

相關文章
相關標籤/搜索