--建立數據庫 create database if not exists sopdm comment ‘this is test database’ with dbproperties(‘creator’=’gxw’,’date’=’2014-11-12’) --數據庫鍵值對屬性信息 location ‘/my/preferred/directory’;數據庫
--查看數據庫的描述信息和文件目錄位置路徑信息 describe database sopdm; --查看數據庫的描述信息和文件目錄位置路徑信息(加上數據庫鍵值對的屬性信息) describe database extended sopdm;app
--刪除數據庫 drop database if exists sopdm; --級聯刪除數據庫(當數據庫還有表時,級聯刪除表後在刪除數據庫),默認是restrict drop database if exists sopdm cascade;工具
--修改數據庫 --只能修改數據庫的鍵值對屬性值。數據庫名和數據庫所在的目錄位置不能修改 alter database sopdm set dmproperties(‘edited-by’=’gaoxianwei’);oop
--建立表 --其中tblproperties做用:按照鍵值對的格式爲表增長額外的文檔說明,也可用來表示數據庫鏈接的必要的元數據信息 --hive會自動增長二個表屬性:last_modified_by(最後修改表的用戶名),last_modified_time(最後一次修改的時間) create table if not exists sopdm.test1(name string comment ‘姓名’,salary float comment ‘薪水’) comment ‘這是一個測試的表’ tblproperties(‘creator’=’me’,’created_at’=’2014-11-13 09:50:33’) location ‘/user/hive/warehouse/sopdm.db/test1’測試
--查看和列舉表的tblproperties屬性信息 show tblproperties table_name;this
--使用like在建立表的時候,拷貝表模式(而無需拷貝數據) create table if not exists sopdm.test2 like sopdm.test1;rest
--查看錶的詳細結構信息(也能夠顯示錶是管理表,仍是外部表。還有分區信息) describe extended sopdm.test1; --使用formatted信息更多些,可讀性更強 describe formatted sopdm.test1;orm
--建立外部表 --刪除表時,表的元數據會被刪除掉,可是數據不會被刪除 --若是數據被多個工具(如pig等)共享,能夠建立外部表 create external table if not exists sopdm.test1( name string comment ‘姓名’, salary float comment ‘薪水’) comment ‘這是一個測試的表’ tblproperties(‘creator’=’me’,’created_at’=’2014-11-13 09:50:33’) location ‘/user/hive/warehouse/sopdm.db/test1’hadoop
--分區表 create table if not exists sopdm.test1( name string comment ‘姓名’, salary float comment ‘薪水’) comment ‘這是一個測試的表’ partitioned by(country string,state string) STORED AS rcfile tblproperties(‘creator’=’me’,’created_at’=’2014-11-13 09:50:33’) location ‘/user/hive/warehouse/sopdm.db/test1’文檔
--查看錶中存在的全部分區 show partitions table_name; --查看錶中特定分區 show partitions table_name partition(country=’US’);
--能夠在表載入數據的時候建立分區 load data local inpath ‘${env:HOME/employees}’ into table employees partition(country=’US’,state=’CA’);
--刪除表 drop table if exists table_name;
--修改表-表重命名 alter table old_table_name rename to new_table_name;
--增長分區 alter table table_name add if not exists partition(year=2011,month=1,day=1) location ‘/logs/2011/01/01’;
--修改分區存儲路徑 alter table table_name partition(year=2011,month=1,day=2) set location ‘/logs/2011/01/02’;
--刪除某個分區 alter table table_name drop if exists partition(year=2011,month=1,day=2);
--修改列信息 alter table table_name change column old_name new_name int comment ‘this is comment’ after severity; --字段移到severity字段以後(移動到第一個位置,使用first關鍵字)
--增長列 alter table table_name add columns(app_name string comment ‘application name’);
--刪除或者替換列 alter table table_name replace columns(hms int comment ‘hhh’);
--修改表屬性 alter table table_name set tblproperties(‘notes’=’this is a notes’);
--修改存儲屬性 alter table table_name partition(year=2011,month=1,day=1) set fileformat sequencefile;
--指定新的SerDe,並指定SerDe屬性 alter table table_name set serde 「com.example.JSONSerDe」 with serdeproperties(‘prop1’=‘value1’, ‘prop2’=‘value2’);
--增長執行「鉤子」——當表中存儲的文在hive以外被修改了,就會觸發鉤子的執行 alter table table_name touch partition(year=2012,month=1,day=1);
--將分區內的文件打成hadoop壓縮包文件,只會下降文件系統中的文件數,減輕NameNode的壓力,而不會減小任何的存儲空間 --使用unarchive替換archive起到反向操做 alter table table_name archive partition(year=2012,month=1,day=1);
--防止分區被刪除和被查詢(使用enable替代disable能夠起到反向的操做目的) alter table table_name partition(year=2012,month=1,day=1) disable no_drop; alter table table_name partition(year=2012,month=1,day=1) disable offline;
--向管理表中裝載數據 -- inpath爲一個目錄,並且這個路徑下不能夠包含任何文件夾 load data local inpath ‘${env:HOME}/table_name’ overwrite into table table_name partition(country=’US’);
--經過查詢語句向表中插入數據 --overwrite是覆蓋,into是追加 insert overwrite table table_name partition(country=’US’) select * from table_name2 tn where tn.cnty=’US’
--高效方式-查詢語句插入多個分區 from table_name2 tn insert overwrite table table_name partition(country=’US’,state=’OR’) select * where tn.cnty=’US’ and tn.st=’OR’ insert overwrite table table_name partition(country=’US’,state=’CA’) select * where tn.cnty=’US’ and tn.st=’CA’
--動態插入分區 --hive根據select語句最後2列肯定分區字段country和state的值(根據位置) insert overwrite table table_name partition(country,state) select …,se.cnty,se.st from employees se;
--動態和靜態分區結合 --country爲靜態分區,state爲動態分區(靜態分區必須在動態分區以前) insert overwrite table table_name partition(country=‘US’,state) select …,se.cnty,se.st from employees se where se.cnty=’US’;
--單個查詢語句中建立表並加載數據 create table table_name1 as select name,salary,address from table_name2 where state=’CA’;
--導出數據——拷貝文件 --若是數據文件剛好是用戶須要的格式,那麼只須要簡單的拷貝文件或文件夾就能夠。 hadoop fs –cp source_path target_path
--導出數據 insert overwrite local directory ‘/tmp/employees’ select name,salary,address from employees se where se.state=’CA’
--導出數據到多個輸出文件夾 from employees se insert overwrite local directory ‘/tmp/or_employees’ select * se where se.cty=’US’ and se.st=’OR’ insert overwrite local directory ‘/tmp/ca_employees’ select * se where se.cty=’US’ and se.st=’CA’