DDL:data definittion language 數(shù)據(jù)定義語言
主要是定義或改變表的結(jié)構(gòu)、數(shù)據(jù)類型、表之間的鏈接和約束等初始化操作
DML:data manipulation language 數(shù)據(jù)操作語言
主要是對數(shù)據(jù)庫的數(shù)據(jù)進行增刪改查操作,如select、insert、delete、update等
一、對數(shù)據(jù)庫的操作
1.創(chuàng)建數(shù)據(jù)庫并指定在hdfs的存儲路徑
create database if not exists hive_db location '/hive_db';
注釋:不指定路徑所創(chuàng)建的數(shù)據(jù)庫默認(rèn)存儲路徑為:“/user/hive/warehouse“
create database if not exists hive_ab;
2.查看數(shù)據(jù)庫信息
1)查看數(shù)據(jù)庫結(jié)構(gòu)
desc database hive_db;
2)添加數(shù)據(jù)庫的描述信息
alter database hive_db set dbproperties('creater'='wyh');
3)查看數(shù)據(jù)庫的拓展信息
desc database extended hive_db;
3.篩選查詢數(shù)據(jù)庫
show database like 'hive*';
4.刪除數(shù)據(jù)庫
drop database wyh;
drop database if exists hive_db;
二、DDL操作
hive中表的種類有很多,如管理表(Manager Table)、外部表(External Table)、分區(qū)表(Partition Table)、分桶表,下面我先介紹前三種表的定義、修改操作。
1.管理表:Hive創(chuàng)建表時默認(rèn)創(chuàng)建的就是管理表,也叫內(nèi)部表,它不擅長數(shù)據(jù)共享,刪除表后數(shù)據(jù)也會被刪除。
創(chuàng)建管理表
create table if not exists emp1(id int,name string) row format delimited fields terminated by '\t';
導(dǎo)入數(shù)據(jù)
load data local inpath '/root/data/emp.txt' into table emp1;
創(chuàng)建新管理表并從emp1表中導(dǎo)入name=wyh的該行數(shù)據(jù)
create table if not exists emp2 as select * from emp1 where name = 'wyh';
查詢表的結(jié)構(gòu)信息:
desc formatted emp2;
2.外部表:Hive不任務(wù)這張表擁有該數(shù)據(jù),所以刪除該表后數(shù)據(jù)不會刪除,當(dāng)再次創(chuàng)建結(jié)構(gòu)與數(shù)據(jù)類型相同的表(無論是外部表還是管理表)時,數(shù)據(jù)會自動關(guān)聯(lián)。但是若第二次創(chuàng)建的是管理表,再次刪除后即使創(chuàng)建相同格式和數(shù)據(jù)類型的表數(shù)據(jù)將不再恢復(fù)!
創(chuàng)建外部表
create external table if not exists student(id int,name string) row format delimited fields terminated by '\t';
導(dǎo)入數(shù)據(jù)
load data local inpath '/root/data/student.txt' into table student;
查看表結(jié)構(gòu)
desc formatted student; (可以從Table Type看到:EXTERNAL_TABLE)
刪除表
drop table if exists student;
3.分區(qū)表:分區(qū)表對應(yīng)HDFS的一個獨立的文件目錄,目錄下是該分區(qū)表所有分區(qū)的目錄,每個分區(qū)目錄下存儲該分區(qū)內(nèi)存儲的數(shù)據(jù)。
創(chuàng)建分區(qū)表
create table dept_partitions(id int,name string,loc string) partitioned by(day string) row format delimited fiedls terminated by '\t';
導(dǎo)入數(shù)據(jù)
load data local inpath '/root/data/dept.txt' into table dept_partition partition(day='1001');
(注意:不能直接導(dǎo)入數(shù)據(jù),必須指定分區(qū))
添加分區(qū)
alter table dept_partition add partition(day='1002');
(添加該分區(qū)后該分區(qū)內(nèi)是沒有數(shù)據(jù)的)
查詢數(shù)據(jù)
select * from dept_partition where day='1001';
select * from dept_partition;
刪除分區(qū)
alter table dept_partition drop partition(day='1002');
alter table dept_partition drop partition(day='1001'),partition(day='1002');
來源:http://www.icode9.com/content-4-160601.html