Hive基本操作(二)

2021-08-07 03:28:41 字數 2872 閱讀 3900

alter table table_name add [if not exists] partition_spec [ location 'location1' ] partition_spec [ location 'location2' ] ...

partition_spec:

: partition (partition_col = partition_col_value, partition_col = partiton_col_value, ...)

eg:alter table c02_clickstat_fatdt1 add

partition (dt='20101202') location '/user/hive/warehouse/c02_clickstat_fatdt1/part20101202'

partition (dt='20101203') location '/user/hive/warehouse/c02_clickstat_fatdt1/part20101203';

alter table table_name drop partition_spec, partition_spec,...

alter table c02_clickstat_fatdt1 drop partition(dt='20101202');

alter table table_name rename to new_table_name

這個命令可以讓使用者為表更名。資料所在的位置和分割槽名並不改變。換而言之,老的表名並未「釋放」,對老表的更改會改變新錶的資料。

alter table table_name change [column] col_old_name col_new_namecolumn_type [comment col_comment] [first|after column_name]

這個命令可以允許改變列名、資料型別、注釋、列位置或者它們的任意組合

alter table table_name add|replace columns (col_name data_type[comment col_comment], ...)

add是代表新增一欄位,字段位置在所有列後面(partition列前);replace則是表示替換表中所有字段。

create view [if not exists] view_name [ (column_name [comment column_comment], ...) ]

[comment view_comment]

[tblproperties (property_name = property_value, ...)]

as select ...

檢視表名

showtables;

檢視表名,部分匹配

showtables 'page.*';

showtables '.*view';

檢視某錶的所有partition,如果沒有就報錯:

showpartitions page_view;

檢視某錶結構:

describe invites;

檢視分割槽內容

selecta.foo from invites a where a.ds ='2008-08-15';

selecta.foo from invites a limit 3;

檢視表分割槽定義

describe extended page_view partition (ds='2008-08-08');

hive裝載資料沒有做任何轉換載入到表中的資料只是進入相應的配置單元表的位置移動資料檔案。純載入操作複製/移動操作。

1 語法

load data [local] inpath 'filepath' [overwrite] intotable tablename [partition (partcol1=val1, partcol2=val2 ...)]

load 操作只是單純的複製/移動操作,將資料檔案移動到 hive 表對應的位置。

載入的目標可以是乙個表或者分割槽。如果表包含分割槽,必須指定每乙個分割槽的分割槽名。

filepath 可以引用乙個檔案(這種情況下,hive 會將檔案移動到表所對應的目錄中)或者是乙個目錄(在這種情況下,hive 會將目錄中的所有檔案移動至表所對應的目錄中)。

如果指定了 local,那麼:

如果沒有指定 local 關鍵字,如果 filepath 指向的是乙個完整的 uri,hive 會直接使用這個 uri。 否則:

如果使用了 overwrite 關鍵字,則目標表(或者分割槽)中的內容(如果有)會被刪除,然後再將 filepath 指向的檔案/目錄中的內容新增到表/分割槽中。

從本地匯入資料到**並追加原表

load data local inpath `/tmp/pv_2008-06-08_us.txt` into table c02  partition (date='2008-06-08', country='us')

從本地匯入資料到**並追加記錄

load data local inpath './examples/files/kv1.txt' into table pokes;

從hdfs匯入資料到**並覆蓋原表

load data inpath '/user/admin/sqlldrdat/cnclickstat/20101101/18/clickstat_gp_fatdt0/0'  into table c02_clickstat_fatdt1 overwrite partition (dt='20101201');

hive基本操作

1.顯示所有資料庫 show databases 2.使用某個資料庫 use xx xx表示某個資料庫名 3.顯示某資料庫下的所有表 show tables 4.檢視表結構 顯示各欄位 desc 表名 5.資料匯出到本地 在hive中操作 insert overwrite local directo...

hive 基本操作

檢視表的詳細資訊 desc 表名 desc formatted 表名 建立外部表 create external table emp ext empno int,ename string,job string,mgr int,hiredate string,sal double,comm doubl...

HIVE基本操作

hive操作 一 建立分割槽 乙個表可以有多個分割槽,為避免過多的小檔案,建議只能對離散字段進行分割槽 create table if not exists stocks ymd date,price open float,price high float,price low float,price...