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 '\t';

  此表名为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 '\t';

  上传数据到分桶表只能通过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

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

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语句改变的是表的元数据信息而不是真正的数据。

  

最新文章

  1. C# 破解 Reflector8.5
  2. ASP.NET ZERO 学习 HangFire的使用
  3. android-获取当前屏幕尺寸信息
  4. Android类库常用类库一览
  5. 实际中理解div布局和浮动
  6. css权威指南(下)
  7. TCP/IP 目录导航
  8. [Flask Security]当不能通过认证的时候制定跳转
  9. java jdk缓存-128~127的Long与Integer
  10. Java IO流之随机读写流RandomAccessFile
  11. 流API--缩减操作
  12. 修改WordPress后台登录地址,提高安全性
  13. SQL*Plus工具使用 sqlplus / as sysdba登录
  14. 企业私有源代码上传github致入侵之大疆案判决了
  15. 【ASP.NET】website转webapplication
  16. 规则引擎 - (二)XOM工程
  17. C#验证ip地址的代码
  18. Jenkins控制台显示乱码
  19. vue之v-model
  20. gulp ( http://markpop.github.io/2014/09/17/Gulp入门教程 )

热门文章

  1. CSS入门基础学习二
  2. 20180709-Java循环结构
  3. [CSP-S模拟测试]:building(模拟)
  4. SecondModel 实现类
  5. 我的Podfile如下
  6. php面试专题---7、文件及目录处理考点
  7. day40—JavaScript多物体运动框架
  8. CentOS 7命令行安装GNOME、KDE图形界面(成功安装验证)
  9. Vagrant 入门 - 项目设置
  10. vlan vtp配置