安裝
- 解壓hive
tar -zxvf apache-hive-1.2.1-bin.tar.gz -C /opt/module/ - 重命名
mv apache-hive-1.2.1-bin/ hive - 復(fù)制配置文件
cp hive-env.sh.template hive-env.sh - 修改配置文件
export HADOOP_HOME=/opt/module/hadoop-2.8.3
export HIVE_CONF_DIR=/opt/module/hive/conf - 配置環(huán)境變量
# HIVE_HOME
export HIVE_HOME=/opt/module/hive
export PATH=$PATH:$HIVE_HOME/bin - 啟動hdfs和yarn
start-dfs.sh
start-yarn.sh - 在HDFS上創(chuàng)建/tmp和/user/hive/warehouse兩個目錄并修改他們的同組權(quán)限可寫
hadoop fs -mkdir /tmp
hadoop fs -mkdir -p /user/hive/warehouse
hadoop fs -chmod g+w /tmp
hadoop fs -chmod g+w /user/hive/warehouse
基本操作
- 啟動hive
hive - 查看數(shù)據(jù)庫
show databases; - 打開默認(rèn)數(shù)據(jù)庫
use default; - 顯示default數(shù)據(jù)庫中的表
show tables; - 創(chuàng)建一張表
create table people(id int, name string); - 顯示數(shù)據(jù)庫中有幾張表
show tables; - 查看表的結(jié)構(gòu)
desc people; - 向表中插入數(shù)據(jù)
insert into people values(1,"ss"); - 查詢表中數(shù)據(jù)
select * from people; - 退出hive
quit;
將本地文件導(dǎo)入hive操作
將本地/opt/module/datas/student.txt 這個文件的數(shù)據(jù)導(dǎo)入到hive的student(id int, name string)表中。
創(chuàng)建表
create table student(id int, name string) row format delimited fields terminated by '\t';
加載數(shù)據(jù)
load data local inpath '/opt/module/datas/student.txt' into table student;
查詢數(shù)據(jù)
select * from student;
Hive元數(shù)據(jù)配置到MySql
遇到的問題
Hive的Metastore默認(rèn)存儲在自帶的derby數(shù)據(jù)庫中,所以當(dāng)開多個客戶端啟動hive,會有異常,如下
Hive元數(shù)據(jù)配置到MySql
- 復(fù)制驅(qū)動到/opt/module/hibe/lib/
cp mysql-connector-java-5.1.27-bin.jar /opt/module/hive/lib/ - 在/opt/module/hive/conf目錄下創(chuàng)建一個hive-site.xml
touch hive-site.xml - 復(fù)制下面內(nèi)容到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://hadoop-100:3306/metastore?createDatabase
IfNotExist=true</value>
<description>JDBC connect string for a JDBC metastore</descr
iption>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
<description>Driver class name for a JDBC metastore</descrip
tion>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
<description>username to use against metastore database</des
cription>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>root</value>
<description>password to use against metastore database</des
cription>
</property>
</configuration>
- 打開多個客戶端測試