![Uploading hive6_786401.png . . .]
](http://upload-images.jianshu.io/upload_images/3068725-de9d189c6ac9218c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
啟動Hadoop各個進程并測試
-
啟動各個進程
hive2.png -
查看hdfs和resourcemanager的web管理界面
hive3.png
hive4.png - 向Hadoop上傳文件進行測試
[wulei@bigdata-00 hadoop-2.5.0]$ bin/hdfs dfs -ls /
[wulei@bigdata-00 hadoop-2.5.0]$ bin/hdfs dfs -mkdir /test
[wulei@bigdata-00 hadoop-2.5.0]$ bin/hdfs dfs -put /etc/hosts /test
[wulei@bigdata-00 hadoop-2.5.0]$ bin/hdfs dfs -ls /test
Found 1 items
-rw-r--r-- 3 wulei supergroup 237 2016-10-22 17:22 /test/hosts
安裝Hive
- 解壓Hive
[wulei@bigdata-00 softwares]$ tar zxf apache-hive-0.13.1-bin.tar.gz -C /opt/modules/
- 配置Hive。需要把conf目錄下的模板配置文件hive-env.sh.template拷貝并重命名為hive-env.sh。并修改文件中的下面兩行,指定hadoop的目錄和加載配置文件的目錄。
HADOOP_HOME=/opt/modules/hadoop-2.5.0
export HIVE_CONF_DIR=/opt/modules/hive-0.13.1/conf
- 在hdfs上創(chuàng)建數(shù)據(jù)倉庫目錄和臨時存儲文件的目錄,并且賦予寫的權(quán)限
[wulei@bigdata-00 hadoop-2.5.0]$ bin/hdfs dfs -mkdir /tmp
[wulei@bigdata-00 hadoop-2.5.0]$ bin/hdfs dfs -mkdir -p /user/hive/warehouse
[wulei@bigdata-00 hadoop-2.5.0]$ bin/hdfs dfs -chmod g+w /tmp
[wulei@bigdata-00 hadoop-2.5.0]$ bin/hdfs dfs -chmod g+w /user/hive/warehouse
-
進入交互式命令,并簡單測試。
hive5.png
安裝Mysql并與Hive建立連接
由于hive默認的derby數(shù)據(jù)庫不能多實例(不能多個客戶端),所以選擇Mysql來存儲Hive中的元數(shù)據(jù)。
- yum安裝mysql-server并啟動進程
[wulei@bigdata-00 ~]$ yum install -y mysql-server
[wulei@bigdata-00 ~]$ sudo /etc/init.d/mysqld start
[wulei@bigdata-00 ~]$ /etc/init.d/mysqld status
mysqld (pid 26011) is running...
- 設置mysql登陸密碼
[wulei@bigdata-00 ~]$ mysqladmin -u root password '123456'
- 進入mysql設置用戶連接權(quán)限,最后重啟服務。
[wulei@bigdata-00 ~]$ mysql -u root -p
Enter password:
mysql> use mysql;
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
mysql> delete from user where user='root' and host'127.0.0.1';
....
mysql> flush privileges;
- Hive配置文件修改
需要在conf目錄下手動創(chuàng)建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:mysql://localhost:3306/metasotre?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>root</value>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>123456</value>
</property>
</configuration>
- 拷貝mysql驅(qū)動包
把mysql驅(qū)動jar包放到Hive安裝目錄的lib下
[wulei@bigdata-00 hive-0.13.1]$ cp /opt/softwares/mysql-connector-java-5.1.27-bin.jar /opt/modules/hive-0.13.1/lib/
- 連接測試
在成功建立連接后,進入mysql數(shù)據(jù)庫,會發(fā)現(xiàn)多了一個metastore數(shù)據(jù)庫,這個數(shù)據(jù)庫就是用來存儲hive的元數(shù)據(jù)信息。

- 其他常用配置
- 在hive-site.xml中配置cli命令顯示數(shù)據(jù)庫名稱和列標題名。
<property>
<name>hive.cli.print.header</name>
<value>true</value>
</property>
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
</property>
- hive默認的日志目錄是在linux本地目錄/tmp/user_name中,為了方便管理,修改日志存放的目錄。把conf目錄下面的hive-log4j.properties.template重命名為hive-log4j.properties,并修改hive.log.dir的值。
hive.log.threshold=ALL
hive.root.logger=INFO,DRFA
hive.log.dir=/opt/modules/hive-0.13.1/logs
hive.log.file=hive.log
8.使用HQL
- 創(chuàng)建數(shù)據(jù)庫以及相關(guān)的操作。創(chuàng)建完一個數(shù)據(jù)庫后會發(fā)現(xiàn)一個數(shù)據(jù)庫對應hadoop上的一個目錄。
hive (default)> show databases;
hive (default)> create database test_db;
hive (default)> use test_db;
hive (test_db)> show tables;

- 創(chuàng)建表并向表中導入數(shù)據(jù)
hive (test_db)> create table student(id int,name string) row format delimited fields terminated by '\t';
hive (test_db)> load data local inpath '/opt/datas/student.txt' into table student;
其中row format delimited fields terminated by '\t'是用來指定表中列與列之間的分隔符。

- 查看表信息
- desc student; 只能查看表的字段和字段類型
- desc extended student; 查看表的完整信息
- desc formatted student; 格式化輸出表的完整信息
-
基本查詢語句。可以發(fā)現(xiàn)在第二條查詢語句本質(zhì)是生成map和reduce任務并執(zhí)行。
hive9.png
hive10.png 查看函數(shù)和函數(shù)用途.
hive (test_db)> show functions;
hive (test_db)> desc function count;
-
在mysql的metastore數(shù)據(jù)庫中查看元數(shù)據(jù)信息
hive11.png
hive12.png







