DDL:data definittion language 數(shù)據(jù)定義語(yǔ)言 主要是定義或改變表的結(jié)構(gòu)、數(shù)據(jù)類型、表之間的鏈接和約束等初始化操作 DML:data manipulation language 數(shù)據(jù)操作語(yǔ)言 主要是對(duì)數(shù)據(jù)庫(kù)的數(shù)據(jù)進(jìn)行增刪改查操作,如select、insert、delete、update等 一、對(duì)數(shù)據(jù)庫(kù)的操作 1.創(chuàng)建數(shù)據(jù)庫(kù)并指定在hdfs的存儲(chǔ)路徑 create database if not exists hive_db location '/hive_db'; 注釋:不指定路徑所創(chuàng)建的數(shù)據(jù)庫(kù)默認(rèn)存儲(chǔ)路徑為:“/user/hive/warehouse“ create database if not exists hive_ab; 2.查看數(shù)據(jù)庫(kù)信息 1)查看數(shù)據(jù)庫(kù)結(jié)構(gòu) desc database hive_db; 2)添加數(shù)據(jù)庫(kù)的描述信息 alter database hive_db set dbproperties('creater'='wyh'); 3)查看數(shù)據(jù)庫(kù)的拓展信息 desc database extended hive_db; 3.篩選查詢數(shù)據(jù)庫(kù) show database like 'hive*'; 4.刪除數(shù)據(jù)庫(kù) drop database wyh; drop database if exists hive_db; 二、DDL操作 hive中表的種類有很多,如管理表(Manager Table)、外部表(External Table)、分區(qū)表(Partition Table)、分桶表,下面我先介紹前三種表的定義、修改操作。 1.管理表:Hive創(chuàng)建表時(shí)默認(rèn)創(chuàng)建的就是管理表,也叫內(nèi)部表,它不擅長(zhǎng)數(shù)據(jù)共享,刪除表后數(shù)據(jù)也會(huì)被刪除。 創(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ù)這張表?yè)碛性摂?shù)據(jù),所以刪除該表后數(shù)據(jù)不會(huì)刪除,當(dāng)再次創(chuàng)建結(jié)構(gòu)與數(shù)據(jù)類型相同的表(無(wú)論是外部表還是管理表)時(shí),數(shù)據(jù)會(huì)自動(dòng)關(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ū)表對(duì)應(yīng)HDFS的一個(gè)獨(dú)立的文件目錄,目錄下是該分區(qū)表所有分區(qū)的目錄,每個(gè)分區(qū)目錄下存儲(chǔ)該分區(qū)內(nèi)存儲(chǔ)的數(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)是沒(mé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'); 來(lái)源:http://www./content-4-160601.html |
|
來(lái)自: 印度阿三17 > 《開(kāi)發(fā)》