首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

大数据HIVE之DDL操作详细解析

大数据HIVE

前言

在学习大数据开发的小伙伴们都知道在HIVE在整个大数据开发中的重要性,他可以称之为大数据中的数据仓库,那么其中的DDL操作各位小伙伴们又熟悉多少呢?在本文将就这个问题做一个详细的讲解。

正文

Hive的DDL操作

DDL:data definittion language  数据定义语言

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

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');

三、修改表

1.修改表名

alter table student rename to students;

2.添加列

alter table students add columns(age int,sex string);

3.更新列(列名和列的数据类型)

alter table student change column age birthday string;

4.替换replace

alter table students replace columns(descccc int);

alter table students replace columns(id int,name string,loc string);

结论

通过本文的讲解各位小伙伴们对于大数据HIVE中的DDL操作有了一定的了解了呢?那么接下来就靠你们自己的努力了。

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190411A04YGF00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券