Hive 史上最全面的大数据学习第九篇(三) Hive表分类

在这里插入图片描述
Hive概述 & 安装方式详解

Hive表操作

Hive表分类

Hive Sql 操作

Hive 自定义函数

Hive On HBase

四、表分类

4.1 管理表(内部表)

内部表与数据库中的Table在概念上是类似的,每一个内部Table在Hive中都有一个相应目录存储数据,所有的Table数据(不包括External Table)都保存在这个目录中。删除表时,元数据与数据都会被删除。

4.2 外部表

在创建表的时候可以指定external关键字创建外部表,外部表对应的文件存储在location指定的目录下,向该目录添加新文件的同时,该表也会读取到该文件(当然文件格式必须跟表定义的一致),删除外部表的同时并不会删除location指定目录下的文件。在删除内部表的时候,内部表的元数据和数据会被一起删除。

删除表,保留表中的数据- 推荐 external

1,zhangsan,2019-11-07,20100,TV|GAME,JIANSHE>001|ZHAOSHAN>002,CHAINA|BJ
2,lisi,2019-11-07,20100,TV|GAME,JIANSHE>002|ZHAOSHAN>006,CHAINA|BJ
3,wangwu,2019-11-07,20100,TV|GAME,JIANSHE>003|ZHAOSHAN>007,CHAINA|BJ
4,ermazi,2019-11-07,20070,TV|GAME,JIANSHE>004|ZHAOSHAN>008,CHAINA|BJ
5,ergouzi,2019-11-07,21000,TV|GAME,JIANSHE>005|ZHAOSHAN>009,CHAINA|BJ
------
create external table t_user_c(
  id int,
  name string,
  birthday date,
  salary double,
  hobbies array<string>,
  card map<string,string>,
  address struct<country:string,city:string> 
)
row format delimited 
fields terminated by ','
collection items terminated by '|'
map keys terminated by '>'
lines terminated by '\n';

-- 指定路径信息
create external table t_user_a(
  id int,
  name string,
  birthday date,
  salary double,
  hobbies array<string>,
  card map<string,string>,
  address struct<country:string,city:string> 
)
row format delimited 
fields terminated by ','
collection items terminated by '|'
map keys terminated by '>'
lines terminated by '\n'
location '/user/hive/warehouse/rechen.db/t_user_c';

4.3 分区表

庞大的数据集可能需要耗费大量的时间去处理。在许多场景下,可以通过分区或切片的方法减少每一次扫描总数据量,这种做法可以显著地改善性能。数据会依照单个或多个列进行分区,通常按照时间、地域或者是商业维度进行分区。比如电影表,分区的依据可以是电影的种类和评级,另外,按照拍摄时间划分可能会得到更一致的结果。为了达到性能表现的一致性,对不同列的划分应该让数据尽可能均匀分布。最好的情况下,分区的划分条件总是能够对应where语句的部分查询条件。

为了对表进行合理的管理以及提高查询效率,Hive可以将表组织成“分区”。分区是表的部分列的集合,可以为频繁使用的数据建立分区,这样查找分区中的数据时就不需要扫描全表,这对于提高查找效率很有帮助。分区表是一种根据“分区列”(partition column)的值对表进行粗略划分的机制。Hive中的每个分区表对应数据库中相应分区列的一个索引,每个分区表对应着表下的一个目录,在HDFS上的表现形式与表在HDFS上的表现形式相同,都是以子目录的形式存在。但是由于HDFS并不支持大量的子目录,这也给分区的使用带来了限制。我们有必要对表中的分区数量进行预估,从而避免因为分区数量过大带来一系列问题。Hive查询通常使用分区的列作为查询条件。这样的做法可以指定MapReduce任务在HDFS中指定的子目录下完成扫描的工作。HDFS的文件目录结构可以像索引一样高效利用。

create external table t_user(
  id int,
  name string,
  birthday date,
  salary double
  country string,
  city string
 )
 row format delimited 
fields terminated by ','
collection items terminated by '|'
map keys terminated by '>'
lines terminated by '\n'
--------------------
create external table t_user_p(
  id int,
  name string,
   birthday date,
  salary double
 )
 partitioned by(country string,city string)
 row format delimited 
 fields terminated by ','
 collection items terminated by '|'
 map keys terminated by '>'
 lines terminated by '\n'
 
 # 导入数据 并且指定分区
 0: jdbc:hive2://CentOS:10000> load data local inpath '/root/t_user_bj' overwrite into table t_user_p partition(country='CHAINA',city='BJ');
 0: jdbc:hive2://CentOS:10000> show partitions t_user_p;
+------------------------+--+
|       partition        |
+------------------------+--+
| country=china/city=bj  |
| country=china/city=sh  |
+------------------------+--+
 # 删除分区
0: jdbc:hive2://CentOS:10000> alter table t_user_p drop partition(country='CHINA',city='BJ');
INFO  : Dropped the partition country=china/city=bj
No rows affected (0.496 seconds)
 # 添加分区
0: jdbc:hive2://CentOS:10000> alter table t_user_p add  partition(country='china',city='bj');

 # 添加分区并且指定分区数据
0: jdbc:hive2://CentOS:10000> alter table t_user_p add  partition(country='CHINA',city='SH') location '/sh';

献给每一个正在努力的我们,就算在忙,也要注意休息和饮食哦!我就是我,一个在互联网跌跌撞撞,摸爬滚打的热忱,给个三连吧~ 还有就是不要只看,多动手才行!

猜你喜欢

转载自blog.csdn.net/artiil/article/details/107423434