1. 程式人生 > >Hive學習(一)Hive的三種搭建方式

Hive學習(一)Hive的三種搭建方式

Hive三種搭建方式

1、解壓apache-hive-1.2.1-bin.tar.gz

tar -zxvf apache-hive-1.2.1-bin.tar.gz 

2、配置環境變數

export HIVE_HOME=/opt/apache-hive-1.2.1
export PATH=$HIVE_HOME/bin:$PATH

一、本地模式(derby)

元資料庫derby與工具都是在本地,這種是最簡單的儲存方式,只需要配置hive-site.xml檔案即可。


1、將hive-default.xml.template更名為hive-site.xml

mv hive-default.xml.template hive-site.xml

2、hive-site.xml的配置:

<?xml version="1.0"?>  
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>  
<configuration>  
<property>  
  <name>javax.jdo.option.ConnectionURL</name>  
  <value>jdbc:derby:;databaseName=metastore_db;create=true</value>  
</property>  
<property>  
  <name>javax.jdo.option.ConnectionDriverName</name>  
  <value>org.apache.derby.jdbc.EmbeddedDriver</value>  
</property>  
<property>  
  <name>hive.metastore.local</name>  
  <value>true</value>  
</property>  
<property>  
  <name>hive.metastore.warehouse.dir</name>  
  <value>/user/hive/warehouse</value>  
</property>   
</configuration>  

切換至/hive/bin目錄下啟動Hive

./hive

注意:
使用derby儲存方式時,執行hive會在當前目錄生成一個derby檔案和一個metastore_db目錄。這種儲存方式的弊端是在同一個目錄下同時只能有一個hive客戶端能使用資料庫。

二、基於Mysql的本地模式

這種儲存方式需要在本地執行一個Mysql伺服器。
1、將Mysql的jar包拷貝到$HIVE_HOME/lib目錄下
2、安裝Mysql
安裝Mysql,並啟動

yum install mysql-server -y

service mysqld start

由於首次安裝,登入不需要密碼

mysql -uroot

修改mysql許可權:刪除多餘對許可權造成影響的資料

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;

重新整理許可權

flush privileges;

新增使用者

CREATE USER 'hive'@'%' IDENTIFIED BY '123';

使用者授權

grant all privileges on hive_meta.* to [email protected]"%" identified by '123';
flush privileges;

3、配置hive-site.xml檔案

<?xml version="1.0"?>  
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>  
<configuration>  
<property>  
  <name>hive.metastore.warehouse.dir</name>  
  <value>/user/hive_remote/warehouse</value>  
</property>  
<property>  
  <name>hive.metastore.local</name>  
  <value>true</value>  
</property>  
<property>  
  <name>javax.jdo.option.ConnectionURL</name>  
  <value>jdbc:mysql://192.168.163.132/hive_meta?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>hive</value>  
</property>  
<property>  
  <name>javax.jdo.option.ConnectionPassword</name>  
  <value>123</value>  
</property>  
</configuration>

切換至/hive/bin目錄下啟動Hive

./hive

注意:

  1. 雖然hive使用者對hive_meta資料庫是有操作許可權的,但是這個資料庫如果不存在,hive使用者也是沒有許可權建立這個資料庫,所以需要提前建立好hive_meta資料庫。
  2. 需要修改使用者名稱密碼,新建的Mysql是沒有密碼的,需要使用者自己更改。

三、基於Mysql的遠端模式

需要將hive-site.xml配置檔案拆為兩部分,分別在服務端和客戶端做如下配置。

服務端配置檔案

<?xml version="1.0"?>  
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>  
<configuration>  
<property>  
  <name>hive.metastore.warehouse.dir</name>  
  <value>/user/hive_remote/warehouse</value>  
</property>  
<property>  
  <name>hive.metastore.local</name>  
  <value>true</value>  
</property>  
<property>  
  <name>javax.jdo.option.ConnectionURL</name>  
  <value>jdbc:mysql://localhost/hive_meta?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>hive</value>  
</property>  
<property>  
  <name>javax.jdo.option.ConnectionPassword</name>  
  <value>password</value>  
</property>  
</configuration>  j

在客戶端啟動meta store service

hive --service metastore

客戶端配置檔案:

<?xml version="1.0"?>  
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>  
<configuration>  
<property>  
  <name>hive.metastore.warehouse.dir</name>  
  <value>/user/hive/warehouse</value>  
</property>     
<property>  
  <name>hive.metastore.local</name>  
  <value>false</value>  
</property>    
<property>  
  <name>hive.metastore.uris</name>  
  <value>thrift://192.168.163.128:9083</value>  
</property>  
</configuration>  

客戶端啟動的時候可能會遇見如下錯誤資訊:

[ERROR] Terminal initialization failed; falling back to unsupported
java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected
at jline.TerminalFactory.create(TerminalFactory.java:101)

錯誤原因:Hadoop jline和Hive的jline版本不一致導致出錯,需要將Hive中的jar包匯入到Hadoop相應的位置。
Hadoop中jline jar包的位置

/hadoop/share/hadoop/yarn/lib

Hive中jline jar包的位置

/hive/lib