hive的数据定义之创建数据库和表

1、数据库的操作

  create database hive_db  //创建数据库hive_db

  create table hive_db.test(字段内容及其格式省略)  //在数据库hive_db中创建test表

  

  create database student_db location '/user/hive/student.db'  //创建数据库student_db,但是在hdfs中显示student.db,在hive控制端中显示studentdb(在有location的情况下)

  create database if not exists hive_db

  show databases like 'hive*'  //结果为hive_db

  

  drop database hive_db  //这种方式只能删除空数据库

  drop database studentdb casecade  //强制删除非空数据库

  describe database hive_db  //显示数据库的信息

  create database teacherdb comment "数据库teacherdb的备注"

2、对表的操作

  create table if not exists hive_db.t1(字段)  //在数据库hive_db中创建表t1

  show tables in hive_db like "t*"  //在数据库hive_db中寻找以t开头的表。

  create table student1 as select * from stu;  //复制表及其数据

  describe extended records;  //查看表信息

  describe formatted records  //查看表详细信息

   2.1、内部表与外部表的相互转换:

  alter table student set tblproperties("EXTERNAL"="TRUE");  //内部表转换为外部表

  alter table student set tblproperties("EXTERNAL"="FALSE");  //外部表转换为内部表

   2.2、分区表(分区在hdfs上其实是目录,分区名不是表结构中的字段名而是在创建表和分区时另外加的):

  create table stu_partition(id int,name string)

  partitioned by (month string)

  row format delimited fields terminated by ' ';

  此表名为stu_partition按照月份来分区。

  上传数据到分区表:

  load data local inpath '/home/hdc/Document/student1.txt' into table stu_partition partition(month="201906");

  分区表查找:

  select * from stu_partition;  //查找分区表中的所有记录;

  select * from stu_partition where month="201906"  //查找分区表中分区名201906中的所有记录  

  查看分区:

  show partitions stu_partition;

  增加分区:

  alter table stu_partition add partition (month="201908");

  alter table stu_partition add partition (month="201909") partition (month="201910");

  删除分区:

  alter table stu_partition drop partition(month="201908");

  alter table stu_partition drop partition(month="201909"),partition (month="201910");

  ps:二级分区指的是2个分区字段,按照字段的顺序来设置分区顺序,例如:partition(month="201909",day="01")就是一个二级分区,其目录结构是day文件夹是month文件夹的子文件夹。

 利用Hadoop和hive命令创建分区的区别:

  其实Hadoop命令创建分区就是在数据仓库中的表下创建一个文件夹,若将数据导入Hadoop命令创建的分区,再利用hive的select语句查询,将查询不到结果。这是因为Hadoop命令创建的分区在hive中没有关于此分区的元数据信息。

  而利用hive命令创建的分区不仅会在hdfs上的hive数据仓库中创建相应的文件夹,而且还将此文件夹在hdfs上的信息(元数据)存储在hive中的matestore数据库中。

 解决方法:

   (1)msck repair table stu_partition;

   (2)alter table stu_partition add partition(month="201911");

    //此方法为分区表在hdfs上创建文件夹和在hive中创建此文件夹的元数据,之前因为利用Hadoop命令手动创建了文件夹故现在只需创建元数据。

  (3)正常上传数据即load data local inpath '/home/hdc/Document/student1.txt' into table stu_partition partition(month="201911");

  2.3、分桶表

  分区表是针对数据的存储路径,分桶表针对的是数据文件。其中分区字段是表外字段,而分桶字段是表内字段。

  create table stu_bucket(

    id int,

    name string

  )clustered by (id) into 4 buckets

  row format delimited fields terminated by ' ';

  上传数据到分桶表只能通过insert方法如下例所示:

  insert into table stu_bucket

  select *from stu_temp;

  利用分桶表对数据进行抽样查询(桶数为z):

  select * from stu_bucket tablesample(bucket x out of y on id)

  注意:x<=y,z%y==0 || y%z==0

  抽样数n=z/y

  从第x桶开始抽取n桶,第一个抽取的是第x桶,第二个桶是x+y

  注意:数据块抽样,按照数据块的百分比抽样,若表的数据大小小于普通的块大小,那么将会返回所有行。

(1)分桶的概念:对于每一个表(table)或者分区, Hive可以进一步组织成桶,也就是说桶是更为细粒度的数据范围划分。Hive也是针对某一列进行桶的组织。Hive采用对列值哈希,然后除以桶的个数求余的方式决定该条记录存放在哪个桶当中。

(2)把表或分区组织成桶的好处:

  a、获得更高的查询处理效率。桶为表加上了额外的结构,Hive 在处理有些查询时能利用这个结构。具体而言,连接两个在(包含连接列的)相同列上划分了桶的表,可以使用 Map 端连接 (Map-side join)高效的实现。比如JOIN操作。对于JOIN操作两个表有一个相同的列,如果对这两个表都进行了桶操作。那么将保存相同列值的桶进行JOIN操作就可以,可以大大较少JOIN的数据量。

  b、使取样(sampling)更高效。在处理大规模数据集时,在开发和修改查询的阶段,如果能在数据集的一小部分数据上试运行查询,会带来很多方便。

  按我的理解,所谓Hive中的分桶,实际就是指的MapReduce中的分区。根据Reduce的数量,分成不同个数的文件。

3、对表的操作

   删除表:

  drop table if exists stu_partition;

   修改表:

  表重命名:alter table stu_partition rename to student_partition;

  修改表中列信息:alter table student_partition change columns id student_id int;

  增加列:alter table student_partition add columns(

        ClassId int commet "备注信息",

        ClassName string comment "备注信息"

      );

  删除或者替换列:alter table student_partition replace columns(

            id string commet "备注信息",

            name string commet "备注信息"

          );//此种替换是指将所用列全部删除再来新建以上两列。、

PS:alter语句改变的是表的元数据信息而不是真正的数据。

原文地址:https://www.cnblogs.com/zhichao123/p/13306726.html