大資料基礎知識學習-----Hive學習筆記(二)Hive安裝環境準備
Hive安裝環境準備
Hive安裝地址
Hive安裝部署
Hive安裝及配置
- 把apache-hive-1.2.1-bin.tar.gz上傳到linux的/opt/software目錄下
- 解壓apache-hive-1.2.1-bin.tar.gz到/opt/module/目錄下面
- [[email protected] software]$ tar -zxvf apache-hive-1.2.1-bin.tar.gz -C /opt/module/
- 修改apache-hive-1.2.1-bin.tar.gz的名稱為hive
- [[email protected]
- [[email protected]
- 修改/opt/module/hive/conf目錄下的hive-env.sh.template名稱為hive-env.sh
- [[email protected] conf]$ mv hive-env.sh.template hive-env.sh
- 配置hive-env.sh檔案
- 配置HADOOP_HOME路徑:export HADOOP_HOME=/opt/module/hadoop-2.7.2
- 配置HIVE_CONF_DIR路徑:export HIVE_CONF_DIR=/opt/module/hive/conf
Hive叢集配置
- 必須啟動hdfs和yarn
- [[email protected] hadoop-2.7.2]$ sbin/start-dfs.sh
- [[email protected] hadoop-2.7.2]$ sbin/start-yarn.sh
- 在HDFS上建立/tmp和/user/hive/warehouse兩個目錄並修改他們的同組許可權可寫
- [[email protected] hadoop-2.7.2]$ bin/hadoop fs -mkdir /tmp
- [[email protected] hadoop-2.7.2]$ bin/hadoop fs -mkdir -p /user/hive/warehouse
[[email protected] hadoop-2.7.2]$ bin/hadoop fs -chmod g+w /tmp
[[email protected] hadoop-2.7.2]$ bin/hadoop fs -chmod g+w /user/hive/warehouse
Hive基本操作
- 啟動hive:
[[email protected] hive]$ bin/hive
- 檢視資料庫:
hive>show databases;
- 開啟預設資料庫:
hive>use default;
- 顯示default資料庫中的表:
hive>show tables;
- 建立一張表:
hive> create table student(id int, name string);
- 顯示資料庫中有幾張表:
hive>show tables;
- 查看錶的結構:
hive>desc student;
- 向表中插入資料:
hive> insert into student values(1000,"ss");
- 查詢表中資料:
hive> select * from student;
- 退出hive:
hive> quit;
將本地檔案匯入Hive例項
需求:將本地/opt/module/datas/student.txt這個目錄下的資料匯入到hive的student(id int, name string)表中。
資料準備
在/opt/module/datas/student.txt這個目錄下準備資料
在/opt/module/目錄下建立datas
- [[email protected] module]$ mkdir datas
在/opt/module/datas/目錄下建立student.txt檔案並新增資料
- [[email protected] module] vi student.txt
1001 zhangshan
1002 lishi
1003 zhaoliu
注意以tab鍵間隔。
- [[email protected] module] vi student.txt
Hive實際操作
- 啟動hive:
[[email protected] hive]$ bin/hive
- 顯示資料庫:
hive>show databases;
- 使用default資料庫:
hive>use default;
- 顯示default資料庫中的表:
hive>show tables;
- 刪除已建立的student表:
hive> drop table student;
- 建立student表, 並宣告檔案分隔符’\t’:
hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
- 載入/opt/module/datas/student.txt 檔案到student資料庫表中:
hive> load data local inpath '/opt/module/datas/student.txt' into table student;
- Hive查詢結果
- hive> select * from student;
OK
1001 zhangshan
1002 lishi
1003 zhaoliu
Time taken: 0.266 seconds, Fetched: 3 row(s)
- hive> select * from student;
Mysql的安裝
安裝包準備
檢視mysql是否安裝,如果安裝了,解除安裝mysql
- 檢視:
rpm -qa|grep mysql
- 解除安裝:
rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64
- 檢視:
解壓mysql-libs.zip檔案到當前目錄
- [[email protected] software]# unzip mysql-libs.zip
- [[email protected] software]# ls
進入到mysql-libs資料夾下,並設定當前使用者執行許可權
[[email protected] mysql-libs]# ll
總用量 76048
-rw-r–r–. 1 root root 18509960 3月 26 2015 MySQL-client-5.6.24-1.el6.x86_64.rpm
-rw-r–r–. 1 root root 3575135 12月 1 2013 mysql-connector-java-5.1.27.tar.gz
-rw-r–r–. 1 root root 55782196 3月 26 2015 MySQL-server-5.6.24-1.el6.x86_64.rpm[[email protected] mysql-libs]# chmod u+x ./*
[[email protected] mysql-libs]# ll
總用量 76048
-rwxr–r–. 1 root root 18509960 3月 26 2015 MySQL-client-5.6.24-1.el6.x86_64.rpm
-rwxr–r–. 1 root root 3575135 12月 1 2013 mysql-connector-java-5.1.27.tar.gz
-rwxr–r–. 1 root root 55782196 3月 26 2015 MySQL-server-5.6.24-1.el6.x86_64.rpm
安裝Mysql伺服器
- 安裝mysql服務端:[[email protected] mysql-libs]# rpm -ivh MySQL-server-5.6.24-1.el6.x86_64.rpm
- 檢視產生的隨機密碼:[[email protected] mysql-libs]# cat /root/.mysql_secret
- 檢視mysql狀態:[[email protected] mysql-libs]# service mysql status
- 啟動mysql:[[email protected] mysql-libs]# service mysql start
安裝Mysql客戶端
- 安裝mysql客戶端:[[email protected] mysql-libs]# rpm -ivh MySQL-client-5.6.24-1.el6.x86_64.rpm
- 連線mysql:[[email protected] mysql-libs]# mysql -uroot -pOEXaQuS8IWkG19Xs
- 修改密碼:mysql>SET PASSWORD=PASSWORD(‘000000’);
- 退出mysql:mysql>exit
Mysql中user表中主機配置
配置只要是root使用者+密碼,在任何主機上都能登入MySQL資料庫。
- 進入mysql:
[[email protected] mysql-libs]# mysql -uroot -p000000
- 顯示資料庫:
mysql>show databases;
- 使用mysql資料庫:
mysql>use mysql;
- 展示mysql資料庫中的所有表:
mysql>show tables;
- 展示user表的結構:
mysql>desc user;
- 查詢user表:
mysql>select User, Host, Password from user;
- 修改user表,把Host表內容修改為%:
mysql>update user set host='%' where host='localhost';
- 刪除root使用者的其他host:
mysql>delete from user where Host='hadoop102 ';
mysql>delete from user where Host='127.0.0.1';
mysql>delete from user where Host='::1';
- 重新整理:
mysql>flush privileges;
- 退出:
mysql> quit;
Hive元資料配置到Mysql
驅動拷貝
- 在/opt/software/mysql-libs目錄下解壓mysql-connector-java-5.1.27.tar.gz驅動包
- [[email protected] mysql-libs]# tar -zxvf mysql-connector-java-5.1.27.tar.gz
- 拷貝/opt/software/mysql-libs/mysql-connector-java-5.1.27目錄下的mysql-connector-java-5.1.27-bin.jar到/opt/module/hive/lib/
- [[email protected] mysql-connector-java-5.1.27]# cp mysql-connector-java-5.1.27-bin.jar /opt/module/hive/lib/
配置Metastore到MySql
在/opt/module/hive/conf目錄下建立一個hive-site.xml
- [[email protected] conf]# touch hive-site.xml
- [[email protected] conf]# vi hive-site.xml
根據官方文件配置引數,拷貝資料到hive-site.xml檔案中。
~~~xml
Hive常用互動命令
- [[email protected]102 hive]$ bin/hive -help
- “-e”不進入hive的互動視窗執行sql語句
- [[email protected] hive]$ bin/hive -e “select id from default.student;”
- “-f”執行指令碼中sql語句
- 在/opt/module/datas目錄下建立hivef.sql檔案
- [[email protected] datas]$ touch hivef.sql
- 檔案中寫入正確的sql語句
- select *from student;
- 執行檔案中的sql語句
- [[email protected] hive]$ bin/hive -f /opt/module/datas/hivef.sql
- 執行檔案中的sql語句並將結果寫入檔案中
- [[email protected] hive]$ bin/hive -f /opt/module/datas/hivef.sql > /opt/module/datas/hive_result.txt
Hive其他命令操作
- 退出hive視窗:
- hive(default)>exit;
- hive(default)>quit;
- 在hive cli命令視窗中如何檢視hdfs檔案系統
- hive(default)>dfs -ls /;
- 在hive cli命令視窗中如何檢視hdfs本地系統
- hive(default)>! ls /opt/module/datas;
- 檢視在hive中輸入的所有歷史命令
- 進入到當前使用者的根目錄/root或/home/atguigu
- 檢視. hivehistory檔案:[[email protected] ~]$ cat .hivehistory
Hive常見屬性配置
Hive資料倉庫位置配置
- Default資料倉庫的最原始位置是在hdfs上的:/user/hive/warehouse路徑下
- 在倉庫目錄下,沒有對預設的資料庫default建立資料夾。如果某張表屬於default資料庫,直接在資料倉庫目錄下建立一個資料夾。
- 修改default資料倉庫原始位置(將hive-default.xml.template如下配置資訊拷貝到hive-site.xml檔案中)
~~~properties
hive.metastore.warehouse.dir
/user/hive/warehouse
location of default database for the warehouse
~~~
- 配置同組使用者有執行許可權
- bin/hdfs dfs -chmod g+w /user/hive/warehouse
顯示當前資料庫,以及查詢表的頭資訊配置
- 在hive-site.xml檔案中新增如下配置資訊,就可以實現顯示當前資料庫,以及查詢表的頭資訊配置。
~~~properties
hive.cli.print.header
true
hive.cli.print.current.db
true
~~~
- 重新啟動hive,對比配置前後差異
Hive執行日誌資訊配置
- Hive的log預設存放在/tmp/atguigu/hive.log目錄下(當前使用者名稱下)。
- 修改hive的log存放日誌到/opt/module/hive/logs
- 修改/opt/module/hive/conf/hive-log4j.properties.template檔名稱為hive-log4j.properties
[[email protected] conf]$ pwd
/opt/module/hive/conf[[email protected] conf]$ mv hive-log4j.properties.template hive-log4j.properties
- 在hive-log4j.properties檔案中修改log存放位置
hive.log.dir=/opt/module/hive/logs
引數配置方式
檢視當前所有的配置資訊:
hive>set;
引數的配置三種方式及優先順序
配置檔案方式
預設配置檔案:hive-default.xml
使用者自定義配置檔案:hive-site.xml注意:
使用者自定義配置會覆蓋預設配置
。另外,Hive也會讀入Hadoop的配置,因為Hive是作為Hadoop的客戶端啟動的,Hive的配置會覆蓋Hadoop的配置。配置檔案的設定對本機啟動的所有Hive程序都有效。命令列引數方式
啟動Hive時,可以在命令列新增-hiveconf param=value來設定引數。
例如:[[email protected] hive]$ bin/hive -hiveconf mapred.reduce.tasks=10;
注意:僅對本次hive啟動有效
檢視引數設定:
hive (default)> set mapred.reduce.tasks;
引數宣告方式
可以在HQL中使用SET關鍵字設定引數
例如:
hive (default)> set mapred.reduce.tasks=100;
注意:僅對本次hive啟動有效。
檢視引數設定:
hive (default)> set mapred.reduce.tasks;
上述三種設定方式的優先順序依次遞增。即配置檔案<命令列引數<引數宣告。注意某些系統級的引數,例如log4j相關的設定,必須用前兩種方式設定,因為那些引數的讀取在會話建立以前已經完成了。