一、了解HIVE
1、是什么
- 是一个可以将sql翻译为MR程序的工具
- 支持用户将HDFS上的文件映射为表结构,用户可以输入SQL进行查询分析
- 将用户定义的库、表结构等信息存储在hive的元数据库中
2、用途
构建大数据体系下的数据仓库
3、使用方式
1)方式一:交互式查询
将hive启动为一个服务: bin/hiveserver ,然后可以在任意一台机器上使用beeline客户端连接hive服务,进行交互式查询
2)方式二:将hive作为命令一次性运行
事先将sql语句写入一个文件比如 q.hql ,然后用hive命令执行:bin/hive -f q.hql
3)方式三:将方式二写入一个xxx.sh脚本中
二、HIVE语句
1、三种执行模式
[root@node01 ~]# hive -e "use default;create table test_1(id int,name string);"
OK
Time taken: 2.453 seconds
OK
Time taken: 1.37 seconds
[root@node01 ~]# hive -e "desc test_1;"
OK
id int
name string
Time taken: 2.939 seconds, Fetched: 2 row(s)
[root@node01 home]# vi a.hql
1,xiaoming,20
2,xiaowang,22
[root@node01 home]# vi /home/userinfo.txt
use default;
create table test_2(id int,name string,age int)
row format delimited
fields terminated by ',';
load data local inpath '/home/userinfo.txt' into table test_2;
select count(*) from test_2;
[root@node01 home]# hive -f a.hql #运行
[root@node01 home]# hive -e "select * from test_2;"
OK
1 xiaoming 20
2 xiaowang 22
NULL NULL NULL
Time taken: 3.608 seconds, Fetched: 3 row(s)
[root@node01 home]# cd /usr/local/src/hive-1.2.1/bin/
[root@node01 bin]# ./hiveserver2
#光标定位到下一行不动
[root@node02 bin]# ./beeline
Beeline version 1.2.1 by Apache Hive
beeline> !connect jdbc:hive2://node01:10000
Connecting to jdbc:hive2://node01:10000
Enter username for jdbc:hive2://node01:10000: root
Enter password for jdbc:hive2://node01:10000:
Connected to: Apache Hive (version 1.2.1)
Driver: Hive JDBC (version 1.2.1)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://node01:10000> #连接到node01服务端
0: jdbc:hive2://node01:10000>
#可以查询node01中的内容(以表格形式)
0: jdbc:hive2://node01:10000> show databases;
+----------------+--+
| database_name |
+----------------+--+
| default |
+----------------+--+
1 row selected (3.375 seconds)
0: jdbc:hive2://node01:10000> show tables;
+-----------+--+
| tab_name |
+-----------+--+
| test_1 |
| test_2 |
+-----------+--+
2 rows selected (0.198 seconds)
0: jdbc:hive2://node01:10000> select * from test_2;
+------------+--------------+-------------+--+
| test_2.id | test_2.name | test_2.age |
+------------+--------------+-------------+--+
| 1 | xiaoming | 20 |
| 2 | xiaowang | 22 |
| NULL | NULL | NULL |
+------------+--------------+-------------+--+
3 rows selected (1.925 seconds)
#node02中每进行一条,node01中都会打印一个OK(因为命令都发到node01上的服务端执行)
[root@node01 bin]# ./hiveserver2
OK
OK
OK
2、建表
1)表定义信息会被记录到hive的元数据中(mysql的hive库)
在hdfs上的hive库目录中创建一个跟表名一致的文件夹
表中没有数据
#查询test_1中的字段信息
[root@node01 home]# hive -e "desc test_1;"
id int
name string
sge int
Time taken: 2.572 seconds, Fetched: 3 row(s)
#有字段id和name,age,我们创建一个文件,好后上传到hdfs对应的test_1表对应的目录下
[root@node01 home]# vi test_1.txt
1,zhangsan,12
2,lisi,32
[root@node01 home]# hadoop fs -put test_1.txt /user/hive/warehouse/test_1
表中有数据
改进
#以上结果
hive> select * from test_1;
OK
NULL NULL NULL
NULL NULL NULL
#建表时没有指定分隔符
#删除表
hive> drop table test_1;
#重新创建一个带有分隔符的表
hive> create table test_1(id string,name string,age int)
> row format delimited
> fields terminated by ',';
#重新上传文件
[root@node01 home]# hadoop fs -put test_1.txt /user/hive/warehouse/test_1
#查看结果
hive> select * from test_1;
OK
1 zhangsan 12
2 lisi 32
#再创建一个文件
[root@node01 home]# vi test_1.txt.1
3,xiaoming,14
4
5,lili3
[root@node01 home]# hadoop fs -put test_1.txt.1 /user/hive/warehouse/test_1
#不填默认为null
hive> select * from test_1;
OK
1 zhangsan 12
2 lisi 32
3 xiaoming 14
4 NULL NULL
5 lili3 NULL
NULL NULL
3、内部表和外部表
1)内部表
#创建表
hive> create table t_2(id int,name string,salary bigint,add string)
> row format delimited
> fields terminated by ','
> location '/aa/bb';
[root@node01 home]# vi /home/salary.txt
1,apple,4,weifang
2,banana,2,hainan
3,orange,4,hunan
4,liulian,100,mangu
#运行
hive> load data local inpath '/home/salary.txt' into table t_2;
hive> select * from t_2;
OK
1 apple 4 weifang
2 banana 2 hainan
3 orange 4 hunan
4 liulian 100 mangu
#删除表
hive> drop table t_2;
hdfs中的文件也会被删除
2)外部表(external)
hive> create external table t_3(id int,name string,salary bigint,add string)
> row format delimited
> fields terminated by ','
> location'/aa/bb';
[root@node01 home]# hadoop fs -put salary.txt /aa/bb/
[root@node01 home]# hadoop fs -cat /aa/bb/salary.txt
1,apple,4,weifang
2,banana,2,hainan
3,orange,4,hunan
4,liulian,100,mangu
hive> select * from t_3;
OK
1 apple 4 weifang
2 banana 2 hainan
3 orange 4 hunan
4 liulian 100 mangu
#删除表
hive> drop table t_3;
数据目录不会删除
3)小结
- 说明: location '/aa/bb’是hdfs的目录
- 区别: 内部表的目录由hive创建在默认的仓库目录下:/user/hive/warehouse/…(也可以指定)
外部表的目录由用户建表时自己指定: location ‘/位置/’
drop一个内部表时,表的元信息和表数据目录都会被删除;
drop一个外部表时,只删除表的元信息,表的数据目录不会删除; - 意义: 通常,一个数据仓库系统,数据总有一个源头,而源头一般是别的应用系统产生的,其目录无定法,为了方便映射,就可以在hive中用外部表进行映射;并且,就算在hive中把这个表给drop掉,也不会删除数据目录,也就不会影响到别的应用系统。
4、 分区关键字 PARTITIONED BY
#创建
hive> create table test_44(ip string,url string,staylong int)
> partitioned by(day string)
> row format delimited
> fields terminated by ',';
[root@node01 home]# vi pv.data.2019-05-10
192.168.9.10,www.a.com,1000
192.168.10.10,www.b.com,100
192.168.11.10,www.c.com,900
192.168.12.10,www.d.com,100
192.168.13.10,www.e.com,2000
hive> load data local inpath '/home/pv.data.2019-05-10' into table test_44 partition(day='2019-05-10');
[root@node01 home]# vi pv.data.2019-05-11
192.168.9.11,www.f.com,100
192.168.10.12,www.g.com,10
192.168.11.13,www.h.com,90
192.168.12.14,www.i.com,10
192.168.13.15,www.g.com,200
hive> load data local inpath '/home/pv.data.2019-05-11' into table test_44 partition(day='2019-05-11');
#查询
hive> select * from test_44;
OK
192.168.9.10 www.a.com 1000 2019-05-10
192.168.10.10 www.b.com 100 2019-05-10
192.168.11.10 www.c.com 900 2019-05-10
192.168.12.10 www.d.com 100 2019-05-10
192.168.13.10 www.e.com 2000 2019-05-10
192.168.9.11 www.f.com 100 2019-05-11
192.168.10.12 www.g.com 10 2019-05-11
192.168.11.13 www.h.com 90 2019-05-11
192.168.12.14 www.i.com 10 2019-05-11
192.168.13.15 www.g.com 200 2019-05-11
Time taken: 0.745 seconds, Fetched: 10 row(s)
#分区查询
hive> select * from test_44 where day