首頁>技術>

Hive 是基於 Hadoop 的一個數據倉庫工具,可以將結構化的資料檔案對映為一張表,並

提供類 SQL 查詢功能。

1.2 Hive 的優缺點1.2.1 優點

1) 操作介面採用類 SQL 語法,提供快速開發的能力(簡單、容易上手)。

2) 避免了去寫 MapReduce,減少開發人員的學習成本。

3) Hive 的執行延遲比較高,因此 Hive 常用於資料分析,對實時性要求不高的場合。

4) Hive 優勢在於處理大資料,對於處理小資料沒有優勢,因為 Hive 的執行延遲比較

高。

5) Hive 支援使用者自定義函式,使用者可以根據自己的需求來實現自己的函式。

1.2.2 缺點

1.Hive 的 HQL 表達能力有限

(1)迭代式演算法無法表達

(2)資料探勘方面不擅長

2.Hive 的效率比較低

(1)Hive 自動生成的 MapReduce 作業,通常情況下不夠智慧化

(2)Hive 調優比較困難,粒度較粗

1.3 Hive 架構原理

圖 1-2 Hive 架構原理

1.使用者介面:Client

CLI(hive shell)、JDBC/ODBC(java 訪問 hive)、WEBUI(瀏覽器訪問 hive)

2.元資料:Metastore

元資料包括:表名、表所屬的資料庫(預設是 default)、表的擁有者、列/分割槽欄位、表

的型別(是否是外部表)、表的資料所在目錄等;

3.Hadoop

使用 HDFS 進行儲存,使用 MapReduce 進行計算。

4.驅動器:Driver

(1)解析器(SQL Parser):將 SQL 字串轉換成抽象語法樹 AST,這一步一般都用

第三方工具庫完成,比如 antlr;對 AST 進行語法分析,比如表是否存在、欄位是否存

在、SQL 語義是否有誤。

(2)編譯器(Physical Plan):將 AST 編譯生成邏輯執行計劃。

(3)最佳化器(Query Optimizer):對邏輯執行計劃進行最佳化。

(4)執行器(Execution):把邏輯執行計劃轉換成可以執行的物理計劃。對於 Hive 來

說,就是 MR/Spark。

第 2 章 Hive 安裝2.1 Hive 安裝部署2.2.1 Hive 安裝及配置

1)修改 apache-hive-1.1.0-bin 的名稱為 hive

[hadoop@master1 software]$ cd /opt/module[hadoop@master1 module]$ ll  total 0  drwxrwxr-x  8 hadoop hadoop 159 Nov 24 10:36 apache-hive-1.1.0-bin  drwxr-xr-x 16 hadoop hadoop 275 Nov 23 06:52 hadoop-2.6.0  drwxr-xr-x  8 hadoop hadoop 176 Nov 22 03:30 jdk1.6.0_45  drwxr-xr-x  8 hadoop hadoop 255 Nov 22 03:29 jdk1.8.0_171[hadoop@master1 module] mv apache-hive-1.2.1-bin/ hive

2)複製/opt/module/hive/conf 目錄下的 hive-env.sh.template 名稱為 hive-env.sh

[hadoop@master1 conf]$ cp hive-env.sh.template hive-env.sh

3)配置 hive-env.sh 檔案

(a)新增配置
export HADOOP_HOME=/opt/module/hadoop-2.6.0
(b)新增配置
export HIVE_CONF_DIR=/opt/module/hive/conf
2.2.2 Hadoop 叢集配置

(1)啟動 hdfs 和 yarn

[hadoop@master1 hadoop-2.6.0]$ sbin/start-dfs.sh[hadoop@master1 hadoop-2.6.0]$ sbin/start-yarn.sh

(2)在 HDFS 上建立/tmp 和/user/hive/warehouse 兩個目錄

[hadoop@master1 hadoop-2.6.0]$ bin/hadoop fs -mkdir /tmp[hadoop@master1 hadoop-2.6.0]$ bin/hadoop fs -mkdir -p /user/hive/warehouse
2.2.3 Hive 基本操作

(1)啟動 hive

[hadoop@master1 hive]$ bin/hive

(2)檢視資料庫

hive> show databases;

(3)開啟預設資料庫

hive> use default;

(4)顯示 default 資料庫中的表

hive> show tables;

(5)建立一張表

hive> create table student(id int, name string);

(6)顯示資料庫中有幾張表

hive> show tables;

(7)查看錶的結構

hive> desc student;

(8)向表中插入資料

hive> insert into student values(1000,"ss");

(9)查詢表中資料

hive> select * from student;

(10)退出 hive

hive> quit;

2.3、 將本地檔案匯入 Hive 案例

需求:

將本地/opt/module/data/student.txt 這個目錄下的資料匯入到 hive 的 student(id int, name

string)表中。

1.資料準備:

2.Hive 實際操作:

(1)啟動 hive

[hadoop@master1 hive]$ bin/hive

(2)顯示資料庫

hive> show databases;

(3)使用 default 資料庫

hive> use default;

(4)顯示 default 資料庫中的表

hive> drop table student;

(6)建立 student 表, 並宣告檔案分隔符’\t’

hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

(7)載入/opt/module/data/student.txt 檔案到 student 資料庫表中。

hive> load data local inpath '/opt/module/datas/student.txt' into table student;

(8)Hive 查詢結果

hive> select * from student;  OK  1001 zhangshan  1002 lishi  1003 zhaoliu  Time taken: 0.266 seconds, Fetched: 3 row(s)
2.4 MySql 安裝2.4.1 安裝包準備2.4.2 檢視 mysql 是否安裝,如果安裝了,解除安裝 mysql(root下)

(1)檢視

[hadoop@master1 ~]# su[root@master1 ~]# rpm -qa|grep mysql    mysql-libs-5.1.73-7.el6.x86_64

(2)解除安裝

[root@master1 ~]# rpm -e --nodeps  mysql-libs-5.1.73-7.el6.x86_64
2.4.3.解壓 mysql壓縮 檔案到當前目錄
[root@master1 software]# tar -xvf mysql-5.7.28-linux-glibc2.12-x86_64.tar //解壓 tar包 #出現兩個 tar.gz檔案,解壓下面這個。另一個刪除即可。[root@master1 software]# tar -xvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
2.4.4、MySQL主目錄處理

在software目錄下移動檔案到/usr/local/mysql:

[root@master1 software]# mv mysql-5.7.28-linux-glibc2.12-x86_64 /usr/local/mysql[root@master1 mysql]# mkdir data
2.4.5、主目錄許可權處理

檢視組和使用者情況:

[root@master1 mysql]# cat /etc/group | grep mysql[root@master1 mysql]# cat /etc/passwd |grep mysql

在檢視就會發現沒有,說明你已經刪掉了。

建立mysql組和mysql使用者。

[root@master1 mysql]# groupadd mysql[root@master1 mysql]# useradd -r -g mysql mysql
2.4.6、建立配置檔案及相關目錄

修改配置檔案:/etc/my.cnf,配置不對的話,後面初始化不全,會拿不到預設密碼。

[root@master1 mysql]# vim /etc/my.cnf #修改內容basedir=/usr/local/mysqldatadir=/usr/local/mysql/dataport = 3306socket=/tmp/mysql.sockpid-file=/tmp/mysqld/mysqld.pidcharacter-set-server = utf8log-error=/var/log/mysqld.log

:wq! 儲存退出。

建立檔案/tmp/mysql.sock:設定使用者組及使用者,授權

[root@master1 etc]# cd /tmp[root@master1 tmp]# touch mysql.sock[root@master1 tmp]# chown mysql:mysql mysql.sock

建立檔案 /tmp/mysqld/mysqld.pid:

mkdir mysqldcd mysqldtouch mysqld.pidcd ..chown -R mysql:mysql mysqldcd mysqldchmod 755 mysqld.pid

建立檔案 /var/log/mysqld.log:

2.4.7、安裝和初始化資料庫

(1)進入bin目錄,初始化資料庫:

[root@master1 bin]# cd /usr/local/mysql/bin/

[root@master1 bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql--datadir=/usr/local/mysql/data

(2)安全啟動:

./mysqld_safe --user=mysql &

是否啟動成功,可以透過檢視mysql程序,ps -ef | grep mysql

預設密碼在mysqld.log日誌裡, 找到後儲存到安全的地方:

cat /var/log/mysqld.log

其中root@localhost: 後面的就是預設密碼,後面登入用.(如果找不到可能預設是空,登入時密碼直接回車,否則可能安裝有問題)

(3)登入mysql:

複製或者輸入mysqld.log中獲得的預設密碼,即可進入mysql命令客戶端。

[root@master1 bin]# cd /usr/local/mysql/bin/[root@master1 bin]# ./mysql -u root -p  Enter password:   Welcome to the MySQL monitor.  Commands end with ; or \g.  Your MySQL connection id is 2  Server version: 5.7.28   Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.   Oracle is a registered trademark of Oracle Corporation and/or its  affiliates. Other names may be trademarks of their respective  owners.   Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.   mysql> 

show databases;

假設密碼修改為:123456

mysql> set password=password("123456");

(4)開機服務啟動設定:

把support-files/mysql.server 複製為/etc/init.d/mysql

檢視是否複製成功

檢視mysql服務是否在服務配置中:

[root@master1 init.d]# chkconfig --list mysql

(5)啟動 或 停止MySQL:

[root@master1 init.d]# service mysql stop    Shutting down MySQL.. SUCCESS! [root@master1 init.d]# service mysql start    Starting MySQL. SUCCESS! [root@master1 init.d]# service mysql status    SUCCESS! MySQL running (8121)
2.4.8、 MySql 中 user 表中主機配置

配置只要是 root 使用者+密碼,在任何主機上都能登入 MySQL 資料庫。

1.進入 mysql

[root@hadoop102 mysql-libs]# mysql -uroot -p123456

2.顯示資料庫

mysql>show databases;

3.使用 mysql 資料庫

mysql>use mysql;

4.展示 mysql 資料庫中的所有表

mysql>show tables;

5.展示 user 表的結構

mysql>desc user;

6.查詢 user

mysql>select User, Host, passwd from user;

7.修改 user 表,把 Host 表內容修改為%

delete from user where Host='hadoop102';delete from user where Host='127.0.0.1';delete from user where Host='::1';

9.重新整理

mysql>flush privileges;

10.退出

mysql>quit;

2.5 Hive 元資料配置到 MySql2.5.1 配置 Metastore 到 到 MySql

1.建立一個 hive-site.xml

[hadoop@master1 conf]$ ls  beeline-log4j.properties.template  hive-default.xml.template  hive-env.sh  hive-env.sh.template  hive-exec-log4j.properties.template  hive-log4j.properties.template[hadoop@master1 conf]$ touch hive-site.xml[hadoop@master1 conf]$ vim hive-site.xml

2.根據官方文件配置引數,複製資料到 hive-site.xml 檔案中

https://cwiki.apache.org/confluence/display/Hive/AdminManual+MetastoreAdmin

3.配置完畢後,如果啟動 hive 異常,可以重新啟動虛擬機器。(重啟後,別忘了啟動 hadoop 叢集)

[hadoop@master1 hive]$ bin/hive
2.5.2 多視窗啟動 Hive 測試

1.先啟動 MySQL

[hadoop@master1 hive]$ mysql -uroot -p123456mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+4 rows in set (0.00 sec)

2.再次開啟多個視窗,分別啟動 hive

[hadoop@master1 hive]$ bin/hive

3.啟動 hive 後,回到 MySQL 視窗檢視資料庫,顯示增加了 metastore 資料庫

mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || metastore || mysql || performance_schema || test |+--------------------+5 rows in set (0.00 sec)
2.6 HiveJDBC 訪問2.6.1 啟動 hiveserver2 服務
[hadoop@master1 hive]$ bin/hiveserver2
2.6.2 另開視窗:啟動 beeline

PS:啟動完 hiveserver後,出現和啟動hive一樣的五條提示資訊。然後停止在本介面

需要,重新在開一個視窗 啟動 beeline

[hadoop@master1 hive]$ bin/beeline  Beeline version 1.2.1 by Apache Hive  beeline>
2.6.3 連線 hiveserver2
beeline> !connect jdbc:hive2://master1:10000scan complete in 2msConnecting to jdbc:hive2://master1:10000Enter username for jdbc:hive2://master1:10000: hadoopEnter password for jdbc:hive2://master1:10000: SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/opt/module/hadoop-2.6.0/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: Found binding in [jar:file:/opt/module/hive/lib/hive-jdbc-1.1.0-standalone.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]Connected to: Apache Hive (version 1.1.0)Driver: Hive JDBC (version 1.1.0)Transaction isolation: TRANSACTION_REPEATABLE_READ0: jdbc:hive2://master1:10000> show databases;+----------------+--+| database_name  |+----------------+--+| default        |+----------------+--+1 row selected (0.992 seconds)0: jdbc:hive2://master1:10000>
2.7 Hive 常用互動命令
[atguigu@hadoop102 hive]$ bin/hive -help

1.“-e”不進入 hive 的互動視窗執行 sql 語句

[hadoop@master1 hive]$ bin/hive -e "select id from student;"

2.“-f”執行指令碼中 sql 語句

(1)在/opt/module/datas 目錄下建立 hivef.sql 檔案

[hadoop@master1 hive]$ touch hivef.sql[hadoop@master1 hive]$ vim hivef.sql #檔案中寫入正確的 sql 語句select * from student;[hadoop@master1 hive]$ mv hivef.sql /opt/module/datas/

(2)執行檔案中的 sql 語句

[hadoop@master1 hive]$ bin/hive -f /opt/module/datas/hivef.sql

(3)執行檔案中的 sql 語句並將結果寫入檔案中

[atguigu@hadoop102 hive]$ bin/hive -f /opt/module/datas/hivef.sql > /opt/module/datas/hive_result.txt

14
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 很實用的執行框命令