DDL:data definittion language  数据定义语言

  主要是定义或改变表的结构、数据类型、表之间的链接和约束等初始化操作

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

DML:data manipulation language  数据操作语言

  主要是对数据库的数据进行增删改查操作,如select、insert、delete、update等

一、对数据库的操作

  1.创建数据库并指定在hdfs的存储路径

  create database if not exists hive_db location '/hive_db';

  注释:不指定路径所创建的数据库默认存储路径为:“/user/hive/warehouse“

  create database if not exists hive_ab;

  2.查看数据库信息

    1)查看数据库结构

    desc database hive_db;

    2)添加数据库的描述信息

    alter database hive_db set dbproperties('creater'='wyh');

    3)查看数据库的拓展信息

    desc database extended hive_db;

  3.筛选查询数据库

  show database like 'hive*';

  4.删除数据库

  drop database wyh;

  drop database if exists hive_db;

二、DDL操作

  hive中表的种类有很多,如管理表(Manager Table)、外部表(External Table)、分区表(Partition Table)、分桶表,下面我先介绍前三种表的定义、修改操作。

  1.管理表:Hive创建表时默认创建的就是管理表,也叫内部表,它不擅长数据共享,删除表后数据也会被删除。

 创建管理表

 create table if not exists emp1(id int,name string) row format delimited fields terminated by '\t';

 导入数据

 load data local inpath '/root/data/emp.txt' into table emp1;

 创建新管理表并从emp1表中导入name=wyh的该行数据

 create table if not exists emp2 as select * from emp1 where name = 'wyh';

 查询表的结构信息:

 desc formatted emp2;

  2.外部表:Hive不任务这张表拥有该数据,所以删除该表后数据不会删除,当再次创建结构与数据类型相同的表(无论是外部表还是管理表)时,数据会自动关联。但是若第二次创建的是管理表,再次删除后即使创建相同格式和数据类型的表数据将不再恢复!

  创建外部表

  create external table if not exists student(id int,name string) row format delimited fields terminated by '\t';

  导入数据

  load data local inpath '/root/data/student.txt' into table student;

  查看表结构

  desc formatted student;  (可以从Table Type看到:EXTERNAL_TABLE)

  删除表

  drop table if exists student;

  3.分区表分区表对应HDFS的一个独立的文件目录,目录下是该分区表所有分区的目录,每个分区目录下存储该分区内存储的数据。

  创建分区表

  create table dept_partitions(id int,name string,loc string) partitioned by(day string) row format delimited fiedls terminated by '\t';

  导入数据

  load data local inpath '/root/data/dept.txt' into table dept_partition partition(day='1001'); 

  (注意:不能直接导入数据,必须指定分区)

  添加分区

  alter table dept_partition add partition(day='1002');

  (添加该分区后该分区内是没有数据的)

  查询数据

  select * from dept_partition where day='1001';

  select * from dept_partition;

  删除分区

  alter table dept_partition drop partition(day='1002');

  alter table dept_partition drop partition(day='1001'),partition(day='1002');

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄