Hive1.2.1安装和使用(基于Hadoop2.6.0)
安装hive,这里使用mysql作为hive的metastore; Ubuntu 15.10虚拟机中安装mysql方法请看:http://kevin12.iteye.com/admin/blogs/2280771 Hadoop2.6.0集群安装:http://kevin12.iteye.com/blog/2273532 1.查看spark 1.6.0版本支持hive的版本从0.
·
安装hive,这里使用mysql作为hive的metastore;
Ubuntu 15.10虚拟机中安装mysql方法请看: http://kevin12.iteye.com/admin/blogs/2280771
Hadoop2.6.0集群安装: http://kevin12.iteye.com/blog/2273532
1.查看spark 1.6.0版本支持hive的版本从0.12.0~1.2.1,这里选择hive的1.2.1版本。
2.去官网下载apache-hive-1.2.1-bin.tar.gz,官网地址:http://hive.apache.org/downloads.html
拷贝到master1虚拟机中的,执行命令解压到当前目录中,然后再移到/usr/local/hive目录中。
配置hive的环境变量
下面贴出我的~.bashrc环境变量配置:
Hive的相关配置如下(红框内):
执行source ~/.bashrc 使配置生效!
3.把mysql的jdbc驱动 mysql-connector-java-5.1.35-bin.jar拷贝到/usr/local/hive/apache-hive-1.2.1-bin/
msyql驱动下载文章结尾!
4.将 hive-default.xml.template拷贝一份出来为hive-site.xml,并修改hive-site.xml文件中下面配置的值:
5.配置hive-env.sh
在最后添加下面的配置:
export HIVE_HOME=/usr/local/hive/apache-hive-1.2.1-bin
export HIVE_CONF_DIR=/usr/local/hive/apache-hive-1.2.1-bin/conf
6.配置 hive-config.sh
在最后面添加下面的配置:
export JAVA_HOME=/usr/local/jdk/jdk1.8.0_60
export HADOOP_HOME=/usr/local/hadoop/hadoop-2.6.0
export SPARK_HOME=/usr/local/spark/spark-1.6.0-bin-hadoop2.6
注意:
hadoop的版本是2.6.0,hive的版本是1.2.1,$HIVE_HOME/lib目录下的jline-2.12.jar比$HADOOP_HOME/share/hadoop/yarn/lib下的jline-0.9.94.jar版本高,版本不一致导致。
拷贝hive中的jline-2.12.jar到$HADOOP_HOME/share/hadoop/yarn/lib下,并重启hadoop即可。
root@master1:/usr/local/hadoop/hadoop-2.6.0/share/hadoop/yarn/lib# mv jline-0.9.94.jar jline-0.9.94.jar20160305
root@master1:/usr/local/hadoop/hadoop-2.6.0/share/hadoop/yarn/lib# cp $HIVE_HOME/lib/jline-2.12.jar ./
7.启动hive
首先要启动hadoop集群,并且保证mysql已经启动。
5.练习使用hive
Hive默认有一个Default的数据库,默认建表会建到该数据库中,表名不区分大小写。
5.1.创建testdb数据库
5.2创建内部表
内部表特点:数据加载到内部表中是,如果数据在本地会在将本地数据拷贝一份到内部LOCATION指定的目录下,如果数据在hdfs上,则会将hdfs中的数据mv到内部表指定的LOCATION中。删除内部表时,会删除相应LOCATION下的数据。
hive在hdfs中的默认位置是/user/hive/warehouse,该位置可以修改,是由配置文件hive-site.xml中属性hive.metastore.warehouse.dir决定的,会在/user/hive/warehouse/testdb.db下创建student目录。
通过浏览器可以查看:
5.3.加载数据到student表中
在linux的/usr/local/hive目录下创建文件,文件名为student,里面包含一列数据可以用数字;
第一种加载数据到student中
注意:使用load加载数据到数据库中是不使用mapreduce的,而桶类型的表用insert要用到mapreduce。
使用select * 不加条件时,不执行MapReduce,执行比较快;最后一行显示的是null,原因是文件中有一行空格;
第二种加载数据到student中的方法
在/usr/local/hive/目录下创建student_1文件,并写入一列数字;
执行命令hadoop fs -put /usr/local/hive/student_1 /user/hive/warehouse/testdb.db/student
或者 hdfs dfs -put /usr/local/hive/student_1 /user/hive/warehouse/testdb.db/student
查看结果:
在浏览器中查看,会将数据放到/user/hive/warehouse/testdb.db/student目录下,如下图:
6.创建表student2,有多个列的情况
创建表,指定分隔符为\t
创建文件,第一列数字,第二列是string类型的,两列之间用\t分割;
上传文件,执行命令
查看student2表中的内容:
注意:内部表会将数据拷贝一份到表目录下面,如果删除内部表元数据,那么该元数据下面的数据也会被删除;
7.创建分区表
创建分区表student3,指定分区为d
创建数据
加载数据到student3中,将student3_1加载到d=1的分区中,将student3_2加载到d=2的分区中。
LOAD DATA LOCAL INPATH '/usr/local/hive/student3_1' INTO TABLE student3 PARTITION (d=1);
LOAD DATA LOCAL INPATH '/usr/local/hive/student3_2' INTO TABLE student3 PARTITION (d=2);
说明:第一列是数据,第二列是分区d;
8. 桶表
(表连接时候使用,根据桶的个数进行取模运算,将不同的数据放到不同的桶中)
创建桶类型的表
create table student4(id int) clustered by(id) into 4 buckets;
必须启用桶表
set hive.enforce.bucketing = true;
插入数据,这里并没有使用load,而是用的insert,insert加载数据使用了mapreduce。
insert into table student4 select id from student3;
从执行过程中可以看出: 桶类型的表用insert要用到mapreduce。
用浏览器查看,创建4个桶,所以生成了4个文件进行存储,分桶的是对4取膜,结果为0的放到了00000_0中,结果为1的放到00000_1中,依次类推;
9.外部表
外部表的特点是:删除表的时候,只删除表定义,不删除表内容。
首先创建/user/hive/data目录,再将/usr/local/hive/student文件上传到/user/hive/data目录中。
创建外部表
create external table student5(id int) location '/user/hive/data/';
说明:如果不指定location,默认的location是/user/hive/warehouse/student5(也即是hdfs://master1:9000/user/hive/warehouse/student6)
查看浏览器,发现外部表创建后并没有在hdfs中产生目录
登录到mysql数据库查看,发现外部表和内部表的LOCATION不一样了。
select * from SDS;
select * from TBLS;
删除外部表student5查看hdfs上的数据是否被删除,
再次查看mysql数据库,发现表结构已经删除,但是数据还是在hdfs上存在。
10.外部分区表
创建外部分区表
将/usr/local/hive/student3_1和student3_2文件分别上传到/user/hive/warehouse/student6/d=1和/user/hive/warehouse/student6/d=2目录中;
分别加载/user/hive/warehouse/student6/d=1和/user/hive/warehouse/student6/d=2中的数据到分区d=1和d=2中;
ALTER TABLE student6 ADD PARTITION (d='1')LOCATION '/user/hive/warehouse/student6/d=1';
11.hive中的视图
创建student6表的视图,并查询视图
create view vw_student6(id) as select id from (select * from student6 where d=1 union all select * from student6 where d=2)a;
其他Hive命令简单介绍
limit命令:select * from t1 limit 3;只会查询出3条记录。
order by 是对结果进行全排序,使用一个reducer,效率较差
sort by 是对每个reducerjinx局部排序,不对整体结果排序,效率较高
distribute by 指的是对mapper的输出按照指定字段,把数据传递到reducer端;
cluster by 子句相当于sort by和distribute by一起操作。
强转:使用函数CAST(id AS long)把id的类型强转为long类型。
详细的可参考官网: http://hive.apache.org/
Ubuntu 15.10虚拟机中安装mysql方法请看: http://kevin12.iteye.com/admin/blogs/2280771
Hadoop2.6.0集群安装: http://kevin12.iteye.com/blog/2273532
1.查看spark 1.6.0版本支持hive的版本从0.12.0~1.2.1,这里选择hive的1.2.1版本。

2.去官网下载apache-hive-1.2.1-bin.tar.gz,官网地址:http://hive.apache.org/downloads.html
拷贝到master1虚拟机中的,执行命令解压到当前目录中,然后再移到/usr/local/hive目录中。
- root@master1:/usr/local/tools# tar -zxvf apache-hive-1.2.1-bin.tar.gz
- root@master1:/usr/local/tools# mv apache-hive-1.2.1-bin/usr/local/hive/
配置hive的环境变量
下面贴出我的~.bashrc环境变量配置:
- export JAVA_HOME=/usr/local/jdk/jdk1.8.0_60
- export JRE_HOME=${JAVA_HOME}/jre
- export SCALA_HOME=/usr/local/scala/scala-2.10.4
- export HADOOP_HOME=/usr/local/hadoop/hadoop-2.6.0
- export HADOOP_CONF_DIR=${HADOOP_HOME}/etc/hadoop
- export HADOOP_COMMON_LIB_NATIVE_DIR=${HADOOP_HOME}/lib/native
- export HADOOP_OPTS="-Djava.library.path=${HADOOP_HOME}/lib"
- export SPARK_HOME=/usr/local/spark/spark-1.6.0-bin-hadoop2.6
- export ZOOKEEPER_HOME=/usr/local/zookeeper/zookeeper-3.4.6
- export HIVE_HOME=/usr/local/hive/apache-hive-1.2.1-bin
- export HIVE_CONF_DIR=${HIVE_HOME}/conf
- export CLASS_PATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:${HIVE_HOME}/lib
- export PATH=.:${JAVA_HOME}/bin:${SCALA_HOME}/bin:${HADOOP_HOME}/bin:${HADOOP_HOME}/sbin:${SPARK_HOME}/bin:${ZOOKEEPER_HOME}/bin:${HIVE_HOME}/bin:$PATH
Hive的相关配置如下(红框内):

执行source ~/.bashrc 使配置生效!
3.把mysql的jdbc驱动 mysql-connector-java-5.1.35-bin.jar拷贝到/usr/local/hive/apache-hive-1.2.1-bin/
msyql驱动下载文章结尾!
4.将 hive-default.xml.template拷贝一份出来为hive-site.xml,并修改hive-site.xml文件中下面配置的值:
- root@master1:/usr/local/hive/apache-hive-1.2.1-bin/conf# cp -a hive-default.xml.template hive-site.xml
- <property>
- <name>javax.jdo.option.ConnectionURL</name>
- <value>jdbc:mysql://master1:3306/hive?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>admin</value>
- </property>
- <property>
- <name>hive.metastore.warehouse.dir</name>
- <value>/user/hive/warehouse</value>
- <description>location of default database for the warehouse</description>
- </property>
- <property>
- <name>hive.metastore.warehouse.dir</name>
- <value>/user/hive/warehouse</value>
- <description>location of default database for the warehouse</description>
- </property>
- <property>
- <name>hive.querylog.location</name>
- <value>/usr/local/hive/iotmp/</value>
- <description>Location of Hive run time structured log file</description>
- </property>
- <property>
- <name>hive.server2.logging.operation.log.location</name>
- <value>/usr/local/hive/iotmp/operation_logs</value>
- <description>Top level directory where operation logs are stored if logging functionality is enabled</description>
- </property>
- <property>
- <name>hive.exec.local.scratchdir</name>
- <value>/usr/local/hive/iotmp/</value>
- <description>Local scratch space for Hive jobs</description>
- </property>
- <property>
- <name>hive.downloaded.resources.dir</name>
- <value>/usr/local/hive/iotmp/${hive.session.id}_resources</value>
- <description>Temporary local directory for added resources in the remote file system.</description>
- </property
5.配置hive-env.sh
在最后添加下面的配置:
- root@master1:/usr/local/hive/apache-hive-1.2.1-bin/conf# cp -a hive-env.sh.template hive-env.sh
export HIVE_HOME=/usr/local/hive/apache-hive-1.2.1-bin
export HIVE_CONF_DIR=/usr/local/hive/apache-hive-1.2.1-bin/conf
6.配置 hive-config.sh
在最后面添加下面的配置:
- root@master1:/usr/local/hive/apache-hive-1.2.1-bin/bin# vim hive-config.sh
export JAVA_HOME=/usr/local/jdk/jdk1.8.0_60
export HADOOP_HOME=/usr/local/hadoop/hadoop-2.6.0
export SPARK_HOME=/usr/local/spark/spark-1.6.0-bin-hadoop2.6
注意:
hadoop的版本是2.6.0,hive的版本是1.2.1,$HIVE_HOME/lib目录下的jline-2.12.jar比$HADOOP_HOME/share/hadoop/yarn/lib下的jline-0.9.94.jar版本高,版本不一致导致。
拷贝hive中的jline-2.12.jar到$HADOOP_HOME/share/hadoop/yarn/lib下,并重启hadoop即可。
root@master1:/usr/local/hadoop/hadoop-2.6.0/share/hadoop/yarn/lib# mv jline-0.9.94.jar jline-0.9.94.jar20160305
root@master1:/usr/local/hadoop/hadoop-2.6.0/share/hadoop/yarn/lib# cp $HIVE_HOME/lib/jline-2.12.jar ./
7.启动hive
首先要启动hadoop集群,并且保证mysql已经启动。

5.练习使用hive
Hive默认有一个Default的数据库,默认建表会建到该数据库中,表名不区分大小写。
5.1.创建testdb数据库
- hive> create database testdb;
- OK
- Time taken: 0.125 seconds
- hive> use testdb;
- OK
- Time taken: 0.068 seconds
- hive> show databases;
- OK
- default
- testdb
- Time taken: 0.026 seconds, Fetched: 2 row(s)
- hive> use testdb;
- OK
- Time taken: 0.059 seconds
- hive>
5.2创建内部表
内部表特点:数据加载到内部表中是,如果数据在本地会在将本地数据拷贝一份到内部LOCATION指定的目录下,如果数据在hdfs上,则会将hdfs中的数据mv到内部表指定的LOCATION中。删除内部表时,会删除相应LOCATION下的数据。
- hive> create table student(id int);
- OK
- Time taken: 0.113 seconds
- hive>
hive在hdfs中的默认位置是/user/hive/warehouse,该位置可以修改,是由配置文件hive-site.xml中属性hive.metastore.warehouse.dir决定的,会在/user/hive/warehouse/testdb.db下创建student目录。
通过浏览器可以查看:

5.3.加载数据到student表中
在linux的/usr/local/hive目录下创建文件,文件名为student,里面包含一列数据可以用数字;

第一种加载数据到student中
注意:使用load加载数据到数据库中是不使用mapreduce的,而桶类型的表用insert要用到mapreduce。
- hive> LOAD DATA LOCAL INPATH '/usr/local/hive/student' INTO TABLE student;
- Loading data to table testdb.student
- Table testdb.student stats: [numFiles=1, totalSize=11]
- OK
- Time taken: 1.717 seconds
- hive> select * from student;
- OK
- 1
- 2
- 3
- 5
- 6
- NULL
- Time taken: 0.572 seconds, Fetched: 6 row(s)
使用select * 不加条件时,不执行MapReduce,执行比较快;最后一行显示的是null,原因是文件中有一行空格;
第二种加载数据到student中的方法
在/usr/local/hive/目录下创建student_1文件,并写入一列数字;
执行命令hadoop fs -put /usr/local/hive/student_1 /user/hive/warehouse/testdb.db/student
或者 hdfs dfs -put /usr/local/hive/student_1 /user/hive/warehouse/testdb.db/student
查看结果:
- hive> select * from student where id is not null;
- OK
- 1
- 2
- 3
- 5
- 6
- 4
- 7
- 8
- 9
- 10
- 11
- Time taken: 0.15 seconds, Fetched: 11 row(s)
- hive>
在浏览器中查看,会将数据放到/user/hive/warehouse/testdb.db/student目录下,如下图:

6.创建表student2,有多个列的情况
创建表,指定分隔符为\t
- hive> CREATE TABLE student2(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
- OK
- Time taken: 0.108 seconds
创建文件,第一列数字,第二列是string类型的,两列之间用\t分割;
上传文件,执行命令
- hdfs dfs -put /usr/local/hive/student2 /user/hive/warehouse/testdb.db/student2

查看student2表中的内容:
- hive> select * from student2;
- OK
- 1 zhangsan
- 2 lisi
- 3 wangwu
- 4 张飞
- 5 孙悟空
- 6 猪八戒
- Time taken: 0.111 seconds, Fetched: 6 row(s)
注意:内部表会将数据拷贝一份到表目录下面,如果删除内部表元数据,那么该元数据下面的数据也会被删除;
7.创建分区表
创建分区表student3,指定分区为d
- hive> CREATE TABLE student3(id int) PARTITIONED BY (d int);
- OK
- Time taken: 0.134 seconds
创建数据

加载数据到student3中,将student3_1加载到d=1的分区中,将student3_2加载到d=2的分区中。
LOAD DATA LOCAL INPATH '/usr/local/hive/student3_1' INTO TABLE student3 PARTITION (d=1);
LOAD DATA LOCAL INPATH '/usr/local/hive/student3_2' INTO TABLE student3 PARTITION (d=2);


说明:第一列是数据,第二列是分区d;
8. 桶表
(表连接时候使用,根据桶的个数进行取模运算,将不同的数据放到不同的桶中)
创建桶类型的表
create table student4(id int) clustered by(id) into 4 buckets;
必须启用桶表
set hive.enforce.bucketing = true;
插入数据,这里并没有使用load,而是用的insert,insert加载数据使用了mapreduce。
insert into table student4 select id from student3;

从执行过程中可以看出: 桶类型的表用insert要用到mapreduce。
用浏览器查看,创建4个桶,所以生成了4个文件进行存储,分桶的是对4取膜,结果为0的放到了00000_0中,结果为1的放到00000_1中,依次类推;


9.外部表
外部表的特点是:删除表的时候,只删除表定义,不删除表内容。
首先创建/user/hive/data目录,再将/usr/local/hive/student文件上传到/user/hive/data目录中。
- root@master1:/usr/local/hive# hdfs dfs -mkdir /user/hive/data/
- 16/03/05 19:36:07 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
- root@master1:/usr/local/hive# hdfs dfs -put student /user/hive/data
- 16/03/05 19:37:10 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
- root@master1:/usr/local/hive#
创建外部表
create external table student5(id int) location '/user/hive/data/';
说明:如果不指定location,默认的location是/user/hive/warehouse/student5(也即是hdfs://master1:9000/user/hive/warehouse/student6)

查看浏览器,发现外部表创建后并没有在hdfs中产生目录

登录到mysql数据库查看,发现外部表和内部表的LOCATION不一样了。

select * from SDS;
select * from TBLS;
删除外部表student5查看hdfs上的数据是否被删除,
- hive> drop table student5;
- OK
- Time taken: 0.105 seconds
再次查看mysql数据库,发现表结构已经删除,但是数据还是在hdfs上存在。


10.外部分区表
创建外部分区表
- drop table if exists student6;
- create EXTERNAL TABLE IF NOT EXISTS student6(
- id int
- )
- PARTITIONED BY (d string);

将/usr/local/hive/student3_1和student3_2文件分别上传到/user/hive/warehouse/student6/d=1和/user/hive/warehouse/student6/d=2目录中;

分别加载/user/hive/warehouse/student6/d=1和/user/hive/warehouse/student6/d=2中的数据到分区d=1和d=2中;
ALTER TABLE student6 ADD PARTITION (d='1')LOCATION '/user/hive/warehouse/student6/d=1';

11.hive中的视图
创建student6表的视图,并查询视图
create view vw_student6(id) as select id from (select * from student6 where d=1 union all select * from student6 where d=2)a;

其他Hive命令简单介绍
limit命令:select * from t1 limit 3;只会查询出3条记录。
order by 是对结果进行全排序,使用一个reducer,效率较差
sort by 是对每个reducerjinx局部排序,不对整体结果排序,效率较高
distribute by 指的是对mapper的输出按照指定字段,把数据传递到reducer端;
cluster by 子句相当于sort by和distribute by一起操作。
强转:使用函数CAST(id AS long)把id的类型强转为long类型。
详细的可参考官网: http://hive.apache.org/
更多推荐
所有评论(0)