1. 程式人生 > >hive資料庫操作詳解,creat,delete,alter,select,load..

hive資料庫操作詳解,creat,delete,alter,select,load..

轉載自 http://gaoxianwei.iteye.com/blog/2160558

--建立資料庫

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;

--刪除資料庫

drop database if exists sopdm;

--級聯刪除資料庫(當資料庫還有表時,級聯刪除表後在刪除資料庫),預設是restrict

drop database if exists sopdm cascade;

--修改資料庫

--只能修改資料庫的鍵值對屬性值。資料庫名和資料庫所在的目錄位置不能修改

alter database sopdm set dmproperties(‘edited-by’=’gaoxianwei’);

--建立表

--其中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;

--使用like在建立表的時候,拷貝表模式(而無需拷貝資料)

create table if not exists sopdm.test2 like sopdm.test1;

--查看錶的詳細結構資訊(也可以顯示錶是管理表,還是外部表。還有分割槽資訊)

describe extended sopdm.test1;

--使用formatted資訊更多些,可讀性更強

describe formatted sopdm.test1;

--建立外部表

--刪除表時,表的元資料會被刪除掉,但是資料不會被刪除

--如果資料被多個工具(如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’

--分割槽表

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’