hive中內 外部表以及修改表操作

2021-10-08 02:18:10 字數 2776 閱讀 4240

hive是基於hadoop的乙個資料倉儲工具,用來進行資料提取、轉化、載入。hive資料倉儲工具能將結構化的資料檔案對映為一張資料庫表,並提供sql查詢功能,能將sql語句轉變成mapreduce任務來執行。hive的資料表分為內部表跟外部表。

hdfs中為所屬資料庫目錄下的子資料夾。資料完全由hive管理。刪除表(元資料)會刪除資料

創表語句:

create

table

ifnot

exists studennt(

id int

,name string

)row format delimited fields

terminated

by'\t'

-- 字段分割符

stored as textfile -- 檔案格式

location '檔案所在路徑';-- 路徑可以拿出來

如果建表時不指定路徑

可以通過 load data local inpath 『路徑』 overwrite into table 表名;

local 可以有可無看資料存放在**

有local:從linux本地下獲取資料,

無local:從hdfs上獲取資料。

overwritre可替換

有overwrite:覆蓋原有的資料

無overwrite:追加

資料儲存在指定的hdfs路徑中;hive不完全管理資料,刪除表(元資料)不會刪除資料

建表語句

create external table

ifnot

exists studennt(

id int

,name string

)row format delimited fields

terminated

by'\t'

-- 字段分割符

stored as textfile -- 檔案格式

location '檔案所在路徑';-- 路徑可以拿出來

select * from table名;可以檢視對映關係(資料表中的資料)

表的儲存位置

內部表存在hdfs指定位置

外部表存在外部,可以通過外部工具檢視,例如sqlyog等等

在刪除表的操作上

內部表刪除時會和原始資料一起刪除

外部表刪除時,原始資料還在

除了內部表和外部表之外,還可以建立臨時表

臨時表是應用程式自動管理在複雜查詢期間生成的中間資料的方法

1)表只對當前session有效,session退出後自動刪除

2)表空間位於/tmp/hive-(安全考慮)

3)如果建立的臨時表表名已存在,實際用的是臨時表

create

temporary

table 表名 (字段)

;create

temporary

table 表名 as..

create

temporary

table 表名 like

..

ctas – as select方式建表

create

table 表名 as

select

*from表名;

ps: ctas不能建立 partition, external, bucket table

cte (ctas with common table expression)

create

table cte_employee as

with

r1 as

(select name from r2 where name =

'..'),

r2 as

(select name from employee where id=

'..'),

r3 as

(select name from employee where id=

'..'

)select

*from r1 union

allselect

*from r3;

like --可以理解為複製表

create

table 表名 like 表名;

修改表名:(多用於資料備份)

alter table 表名 rename to 新錶名

修改表注釋:

alter table 表名 set tblproperties(『comment』=『new comment』)

修改檔案分隔符:

alter table 表名 set tblproperties(『filed.delim』=』分割方式』)

修改表檔案格式:

alter table 表名 set fileformat rcfile(修改為rcfile格式)

修改表列相關操作

1) 修改列名:

alter table 表名 change 舊名 新名 字段型別;

2)新增一列:

alter table 表名 add columns(新增的列名 字段型別)

3)替換一列:

alter table 表名 replace columns(列名 字段型別)

ps:會替換所有列,要謹慎使用

Hive知識之內外部表和分割槽表

2 分割槽表 1 內外部表create external table ifnot exists 表名.1.1 內部表 1.2 外部表 2 分割槽表create external table ifnot exists 表名.partitioned by 列名 資料型別 comment 本列注釋 2.1...

hive 內外部表載入hdfs資料位置情況

hive 表載入hdfs資料 情況1 建立內部表 載入前hdfs資料 匯入資料 總結 建立內部表 load 資料 hdfs資料會進行遷移 遷移位置 到了 hive表目錄 user hive warehouse tmp.db 情況二建立外部表 create external table if not ...

css 樣式表的引入(內部 行內 外部)

1 內部樣式表 doctype html html head meta charset utf 8 title css title style type text css class1 class2 class3 style head body bgcolor aquamarine center d...