Hive从入门到精通10:Hive的数据模型

Hive的数据是存储在HDFS上的,且没有专门的数据存储格式。Hive可以直接加载文本文件来创建表,只需要在创建时指定列分隔符和行分隔符即可。Hive的数据模型主要有:数据库、文件、表和视图等。Hive中的表又分为4类:内部表,分区表、桶表和外部表。

小技巧:

如果不想在执行HQL语句时打印日志,可以用Hive的静默模式(-S选项, Silence)启动Hive:

[root@master ~]# hive -S
hive>

1.Inner Table (内部表)

与数据库中的Table在概念上是类型的;Hive的每一个Table在HDFS上都有一个相应的目录存储数据;一个Table的所有数据都保存在该目录下(不包括外部表);删除一个Table时,数据和元数据都会被删除。

示例:创建一张内部表emp_in(注意:默认分隔符是Tab键,这里导入csv文件需要指定分隔符为逗号)

hive> create table emp_in (
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal int,
comm int,
deptno int
)
row format delimited fields terminated by ',';
OK
Time taken: 1.057 seconds
hive> show tables;
OK
emp_in
Time taken: 0.005 seconds, Fetched: 1 row(s)

使用load命令往emp_in表导入数据:/input/emp.csv,load命令既可以导入HDFS上的数据,也可以导入本地文件系统上的数据,但是需要注意的是,load命令相当于Ctrl+X和Ctrl+V,即使用load导入数据之后,源数据就不存在了。

导入HDFS上的数据:

hive> load data inpath '/input/emp.csv' into table emp_in;
Loading data to table default.emp_in
Table default.emp_in stats: [numFiles-1, numRows-0, totalSize-617, rawDataSize-0]
OK
Time taken: 0.759 seconds

导入本地系统上的数据:

hive> load data local inpath '/input/emp.csv' into table emp_in;

查询表数据:

hive> select * from emp_in;(简单:不会转成MapReduce任务)
hive> select * from emp_in where deptno = 10;(复杂:会转成MapReduce任务)
hive> select * from emp_in order by sal;(复杂:会转成MapReduce任务)

2.Partition Table (分区表)

Partition对应于数据库的列的密集索引;Table的一个Partition对应与Table目录下的一个子目录,所有本Partition的数据都存储在该子目录下面;使用Partition可以提高查询效率。

示例:创建一张分区表,按照员工的部门号进行分区

hive> create table emp_part (
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal int,
comm int
)
partitioned by (deptno int)
row format delimited fields terminated by ',';

使用insert into … select …语句往分区表插入数据:/input/emp.csv,会转成MapReduce任务。

hive> insert into table emp_part partition(deptno = 10)
select empno,ename,mgr,hiredate,sal,comm from emp_in where deptno = 10;
hive> insert into table emp_part partition(deptno = 20)
select empno,ename,mgr,hiredate,sal,comm from emp_in where deptno = 20;
hive> insert into table emp_part partition(deptno = 30)
select empno,ename,mgr,hiredate,sal,comm from emp_in where deptno = 30;

分区表目录下是以分区建立的子目录:

[root@master ~]# hdfs dfs -ls /hive/warehouse/emp_part

建立分区可以提高查询效率:

示例:查询10号部门的员工——对比分区表和内部表的查询计划

hive> explain select * from emp_in where deptno = 10;
STAGE DEPENDENCIES:
Stage-1 is a root stage
Stage-0 is a root stage

STAGE PLANS:
Stage: Stage-1
Map Reduce
Map Operator Tree:
TableScan
alias: emp_in
Statistics: Num rows: 1 Data size: 617 Basic stats: COMPLETE Column stats: NONE
Filter Operator
predicate: (deptno = 10) (type: boolean)
Statistics: Num rows: 0 Data size: 0 Basic stats: NONE Column stats: NONE
Select Operator
expressions: empno (type: int), ename (type: string), job (type: string), mgr (type: int), hiredate (type: string), sal (type: int), comm (type: int), deptno (type: int)
outputColumnNames: _col0, _col1, _col2, _col3, _col4, _col5, _col6, _col7
Statistics: Num rows: 0 Data size: 0 Basic stats: NONE Column stats: NONE
File Output Operator
compressed: false
Statistics: Num rows: 0 Data size: 0 Basic stats: NONE Column stats: NONE
table:
input format: org.apache.hadoop.mapred.TextInputFormat
output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe

Stage: Stage-0
Fetch Operator
limit: -1
hive> explain select * from emp_part where deptno = 10;
STAGE DEPENDENCIES:
Stage-0 is a root stage

STAGE PLANS:
Stage: Stage-0
Fetch Operator
limit: -1
Processor Tree:
TableScan
alias: emp_part
Statistics: Num rows: 3 Data size: 121 Basic stats: COMPLETE Column stats: NONE
Select Operator
expressions: empno (type: int), ename (type: string), job (type: string), mgr (type: int), hiredate (type: string), sal (type: int), comm (type: int), deptno (type: int)
outputColumnNames: _col0, _col1, _col2, _col3, _col4, _col5, _col6, _col7
Statistics: Num rows: 3 Data size: 121 Basic stats: COMPLETE Column stats: NONE
ListSink

通过打印出来的执行计划可以看到,内部表扫描的数据量是617个字节,分区表扫描的数据量是121个字节,因此建立分区表可以大大提高查询效率(只限于分区上查询)。

3.Bucket Table (桶表)

桶表是对数据的键值进行Hash,然后存储在不同的文件中;使用桶表需要设置环境变量set hive.enforce.bucketing=true;。当然使用桶表也可以提高查询效率,尤其是在分区后再分桶,可以进一步提高查询效率。注意:与分区不同,桶是一个文件,不是目录。

使用桶表的前提,打开开关:

hive> set hive.enforce.bucketing=true;

示例:创建一个桶表,根据员工的职位(job)分桶。

hive> create table emp_bucket (
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal int,
comm int,
deptno int
)
clustered by (job) into 4 buckets
row format delimited fields terminated by ',';

往桶表插入数据:

hive> insert into table emp_bucket select * from emp_in;
[root@master ~]# hdfs dfs -ls /hive/warehouse/emp_bucket
Found 4 items
-rw-r--r-- 1 root root 45 2018-08-28 11:12 /hive/warehouse/emp_bucket/000000_0
-rw-r--r-- 1 root root 304 2018-08-28 11:12 /hive/warehouse/emp_bucket/000001_0
-rw-r--r-- 1 root root 276 2018-08-28 11:12 /hive/warehouse/emp_bucket/000002_0
-rw-r--r-- 1 root root 0 2018-08-28 11:12 /hive/warehouse/emp_bucket/000003_0

[root@master ~]# hdfs dfs -car /hive/warehouse/emp_bucket/000000_0
7839,KING,PRESIDENT,\N,1981/11/17,5000,\N,10

hive> select * from emp_bucket;

4.External Table (外部表)

外部表的实际数据不是存储在数据仓库目录下,而是直接存储在HDFS上;外部表的元数据和内部表是一样的,都是存储在数据库中;创建外部表就是建立一个指向HDFS上已有数据的连接,加载数据和创建表同时完成,并不会将实际数据移动到数据仓库中;删除外部表时,只是删除了该连接和相应的元信息,实际数据并不会删除。

示例:创建外部表student_ext,指向HDFS上已有的目录/input/students

[root@master ~]# hdfs dfs -ls /input/students
Found 3 items
-rw-r--r-- 1 root root 19 2018-08-28 11:12 /input/students/student01.txt
-rw-r--r-- 1 root root 19 2018-08-28 11:12 /input/students/student02.txt
-rw-r--r-- 1 root root 19 2018-08-28 11:12 /input/students/student03.txt
[root@master ~]# hdfs dfs -cat /input/students/student01.txt
1,Tom,23
[root@master ~]# hdfs dfs -cat /input/students/student02.txt
2,Mary,21
[root@master ~]# hdfs dfs -cat /input/students/student03.txt
3,Jack,22

hive> create table students_ext(sid int, sname string, sage int)
row format delimited fields terminated by ','
location '/input/students';

hive> select * from students_ext;
1 Tom 23
2 Mary 21
3 Jack 22

外部表指向的目录下的数据有变化时,外部表的数据也跟着变化:

[root@master ~]# hdfs dfs -rmr /input/students/student03.txt
hive> select * from students_ext;
1 Tom 23
2 Mary 21

删除外部表时,原来指向的目录下的数据不删除

hive> drop table students_ext;
hive> select * from students_ext;(出错:表不存在)
[root@master ~]# hdfs dfs -ls /input/students
Found 2 items
-rw-r--r-- 1 root root 19 2018-08-28 11:12 /input/students/student01.txt
-rw-r--r-- 1 root root 19 2018-08-28 11:12 /input/students/student02.txt

重建外部表,数据又回来了

hive> create table students_ext(sid int, sname string, sage int)
row format delimited fields terminated by ','
location '/input/students';

hive> select * from students_ext;
1 Tom 23
2 Mary 21

5.View(视图)

Hive 中的视图(View),是一种虚表(不保存数据,用于简化查询),是一个逻辑概念,可以跨越多张表;视图建立在已有表的基础上,视图赖以建立的这些表成为基表;使用视图可以简化复杂查询。

示例:查询10号部门的员工

hive> select * from emp_in where deptno = 10;
7782 CLARK MANAGER 7839 1981/6/9 2450 NULL 10
7839 KING PRESIDENT NULL 1981/11/17 5000 NULL 10
7934 MILLER CLERK 7782 1982/1/23 1300 NULL 10

hive> create view10 as select * from emp_in where deptno = 10;

hive> select * from view10;
7782 CLARK MANAGER 7839 1981/6/9 2450 NULL 10
7839 KING PRESIDENT NULL 1981/11/17 5000 NULL 10
7934 MILLER CLERK 7782 1982/1/23 1300 NULL 10

所有数据模型的元信息都被保存到MySQL数据库中:

  • MySQL->hive->TBL:保存Hive中表的信息
  • MySQL->hive->COLUMNS:保存Hive中列的元信息
  • MySQL->hive->PARTION%:保存Hive中分区的元信息

你可能感兴趣的:(Hive从入门到精通10:Hive的数据模型)