首页 技术 正文
技术 2022年11月14日
0 收藏 439 点赞 2,736 浏览 2317 个字

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

  注意:第二次替换后列的数据类型与第一次相同,数据会恢复!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289