【完美解決】Spark-SQL、Hive多 Metastore、多后端、多庫


【完美解決】Spark-SQL、Hive多 Metastore、多后端、多庫

 

【完美解決】Spark-SQL、Hive多 Metastore、多后端、多庫

SparkSQL 支持同時連接多種 Metastore,包括Atlas2(PB),Hive 0.12+幾種格式。用戶可以在一條SQL語句中操作來自多個 Metastore 的表。

配置 Metastore

按照正常的使用方式配置 conf/hive-site.xml

比如配置訪問 mysql:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property><name>mysql.metastore.zk.server</name><value>zk.weilx.com:2183</value><source>programatically</source></property>
<property><name>mysql.metastore.zk.path</name><value>/biglog/metaspark</value><source>programatically</source></property>
<property><name>hive.metastore.type</name><value>mysql</value><source>programatically</source></property>
<property><name>mysql.identity.user</name><value>test</value><source>programatically</source></property>
</configuration>

執行 spark-sql:

$ ./bin/spark-sql
spark-sql> show databases;
OK
default
mysql
Time taken: 2.301 seconds, Fetched 5 row(s)

可以看到已經可以正常訪問 mysql 了。

添加一個 Metastore

添加一個新的 metastore 需要在 conf/ 中新增加一個配置文件,比如 hive-site2.xml(文件名無限制),里邊配置新的metastore,實例內容如下:

<property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://10.xx.xx.xx/hive13?createDatabaseIfNotExist=true</value>
</property>
<property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>com.mysql.jdbc.Driver</value>
</property>
<property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>test</value>
</property>
<property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>test</value>
</property>
<property>
    <name>hive.metastore.type</name>
    <value>hive</value>
</property>

然后啟動 spark-sql 客戶端:

# 添加新的配置文件到環境中
spark-sql> set metaclient.config.hive2=hive-site2.xml;
Time taken: 0.104 seconds
  
# 為 hive2.default 數據庫指定別名 default2
spark-sql> set metaclient.alias.default2=hive2.default;
Time taken: 0.109 seconds
 
# 使用 default2 數據庫
spark-sql> use default2;
spark-sql> show tables;
ares_test       false
inserttest      false
people  false
src     false
srczzz  false
Time taken: 0.433 seconds, Fetched 5 row(s)

費元星的hive-site.xml 配置:

<configuration>
<!--如果沒有配置說明,等於耍流氓 by feiyuanxing-->
<property>
<name>hive.metastore.warehouse.dir</name>
<value>hdfs://IP:prot/app/ns/df</value>
<description>location of default database for the warehouse</description>
</property>
<property>
<name>hive.exec.scratchdir</name>
<value>hdfs://IP:prot/app/ns/df/tmp/hive-${user.name}</value>
<description>Scratch space for Hive jobs</description>
</property>
  <property>
    <name>hive.security.authorization.enabled</name>
    <value>false</value>
  </property>
<!-- -->
<property>
    <name>hive.metastore.client.connect.retry.delay</name>
    <value>-1</value>
  </property>

<property>
    <name>hive.cli.print.current.db</name>
    <value>true</value>
  </property>

  <property>
    <name>hive.metastore.thrift.framed.transport.enabled</name>
    <value>true</value>
  </property>
  <property>
    <name>hive.metastore.use.combined</name>
    <value>true</value>
  </property>

<!-- 連接mysql -->

<property>
<name>metaclient.config.mysql</name>
<value>hive-site-mysql.xml</value>
</property>
<property>
<name>metaclient.alias.mysql</name>
<value>mysql.mysql</value>
</property>
<property>
<name>metaclient.config.hive</name>
<value>hive-site-hive.xml</value>
</property>
<property>
<name>metaclient.alias.hive</name>
<value>hive.hive</value>
</property>

</configuration>

 

 

跨 Metastore 操作

經過上邊兩步配置,當前系統中一共存在兩個 metastore: mysql 和 hive2. 而且我們通過為 hive2 中的 default 數據指定別名為 default2 避免了命名沖突的問題,那么現在就可以同時操作兩個數據庫中的表了。比如:

select T1.event_id, T1.event_time from default.test_table T1 join default2.test_table2 T2 on T1.event_id == T2.event_id;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM