1. 程式人生 > 實用技巧 >Hive資料倉庫操作

Hive資料倉庫操作

Hive資料庫安裝的三種方式

內嵌模式安裝

內嵌derby資料庫:一個會話連線,常用於簡單測試

  1. 啟動hadoop叢集

    1 start-dfs.sh
    2 start-yarn.sh
    3 jps

  2. 查詢hive壓縮包

    1 find / -name *hive*.tar.gz

  3. 解壓hive壓縮包到/opt目錄下

    1 tar -zxvf /root/experiment/file/apache-hive-2.1.1-bin.tar.gz -C /opt/
    2 # 檢視是否解壓成功
    3 ls /opt/
    4 # 重新命名apache-hive-2.1.1-bin為hive
    5 mv apache-hive-2.1
    .1-bin hive 6 # 檢視是否重新命名成功 7 ls /opt/
  4. 在profile中配置hive環境變數

    1 vi /etc/profile
    2 # 在HADOOP環境變數下方新增HIVE環境變數
    3 export HIVE_HOME=/opt/hive
    4 export PATH=$HIVE_HOME/bin:$PATH
    5 # 使profile檔案配置生效
    6 source /etc/profile
    7 # 檢視所有環境變數,是否有/opt/hive/bin
    8 echo $PATH

  5. 初始化Hive元資料

    1 schematool -dbType derby -initSchema
    2
    # 檢視資料庫檔案 3 ls

    初始化結束後會生成derby.log 和 metastore_db目錄(儲存derby資料庫,儲存元資料),資料存放在hdfs上

  6. 進入Hive Shell

    1 hive

  7. Hive Shell操作

     1 # 顯示資料庫
     2 show databases;
     3 # 顯示錶
     4 show tables;
     5 # 顯示函式
     6 show functions;
     7 # 檢視HDFS
     8 dfs -ls -R /;
     9 # 退出Hive Shell
    10 quit;
    11 exit;

本地模式安裝

  1. 進入Mysql

    1 mysql
    
    2 # 建立資料庫 3 create database hive; 4 # 檢視是否建立成功 5 show databases;

  2. Mysql授權

     1 grant all privileges on *.* to 'root'@'master' identified by 'root';
     2 grant all privileges on *.* to 'root'@'%' identified by 'root';
     3 # 刷新系統許可權相關表
     4 flush privileges;
     5 # 檢視許可權
     6 show databases;
     7 use mysql;
     8 show tables;
     9 desc user;
    10 select Host,User,Super_priv from user;
    11 # 退出Mysql
    12 quit();

  3. 拷貝Hive需要的mysql依賴包mysql-connector-java-5.1.42.jar 至hive/lib目錄下

    1 # 查詢jar包的位置
    2 find / -name mysql*.jar
    3 # 拷貝jar包
    4 cp /root/experiment/file/mysql-connector-java-5.1.42.jar /opt/hive/lib
    5 # 檢視/opt/hive/lib目錄是否有
    6 ls /opt/hive/lib

  4. 進入hive的conf目錄下,配置hive相關配置檔案引數

    1 cd /opt/hive/conf
    2 # 檢視conf目錄下內容
    3 ls

    • hive-site.xml

       1 cp hive-default.xml.template hive-site.xml
       2 # 檢視是否生成hive-site.xml
       3 ls
       4 # 配置hive-site.xml檔案
       5 vi hive-site.xml
       6 # 查詢ConnectionURL
       7 :?ConnectionURL
       8 # 顯示行號
       9 :set nu
      10 # 刪除無關內容
      11 :18,498d
      12 :21,25d
      13 :22,4862d
      14 # 取消顯示行號
      15 :set nonu

      配置檔案

       1 <configuration>
       2     <property>
       3         <name>javax.jdo.option.ConnectionURL</name>
       4         <value>jdbc:mysql://master:3306/hive</value>
       5     </property>
       6     <property>
       7         <name>javax.jdo.option.ConnectionDriverName</name>
       8         <value>com.mysql.jdbc.Driver</value>
       9     </property>
      10     <property>
      11         <name>javax.jdo.option.ConnectionUserName</name>
      12         <value>root</value>
      13     </property>
      14     <property>
      15         <name>javax.jdo.option.ConnectionPassword</name>
      16         <value>root</value>
      17     </property>
      18 </configuration>

  5. 初始化資料庫

    1 schematool -dbType mysql -initSchema
    2 hive
    3 show tables;
    4 show functions;

  6. 操作Hive

    在Mysql中檢視TBLS表來檢視Hive是否建表 Hive中建立的表的欄位資訊會自動存入到MySQL的hive資料庫COLUMNS_V2表中

遠端模式安裝

Hive Shell操作表和資料

  1. 上傳資料到叢集上

  2. 建立表

     1 create table stu_info(
     2 id int,
     3 name string,
     4 age int) row format delimited
     5 fields terminated by '\\\\t';
     6 # 指定資料庫位置,目錄會自動建立
     7 create table stu_info(
     8 id int,
     9 name string,
    10 age int) location '/pcc/stu_info';
    11 # 將另一個表的資料給一個新表
    12 create table stu_info row format delimited
    13 fields terminated by ',' AS select * from stu_info;
    14 # 建立分割槽表
    15 create table stu_info_0 (id int, name string) partitioned by(age int)
    16 row format delimited fields terminated by ',';

  3. 匯入資料

    1 # 匯入叢集檔案
    2 load data inpath '/pcc/file' overwrite into table stu_info;
    3 # 匯入本地檔案
    4 load data local inpath '/home/file' overwrite into table stu_info;
    5 # 向分割槽中匯入資料
    6 insert overwrite table stu_info_0 patition(age=22)
    7 select id, name from stu_info where age=22;

  4. 匯出資料

    1 create table t1 as select * from stu_info where gender = 'male';