hive 表資料載入 表刪除試驗

2021-07-10 07:08:26 字數 3997 閱讀 2093

1. 非分割槽表

(1)load 載入資料

本地文字檔案a.txt中有一行'aaa',執行下面的命令。

create table t1 (name string);

load data local inpath '/home/grid/a.txt' into table t1;

select * from t1;

dfs -ls /user/hive/warehouse/test.db/t1;

執行命令及結果如圖1所示。

圖1 可以看到,向表中載入了資料'aaa',生成了資料檔案/user/hive/warehouse/test.db/t1/a.txt

在a.txt中新增一行'bbb',然後在執行下面的命令。

load data local inpath '/home/grid/a.txt' into table t1;

select * from t1;

dfs -ls /user/hive/warehouse/test.db/t1;

執行命令及結果如圖2所示。

圖2 可以看到,現在表中有三條資料,新生成了資料檔案/user/hive/warehouse/test.db/t1/a_copy_1.txt。

(2)load overwrite 載入資料

執行下面的命令。

create table t2 (name string);

load data local inpath '/home/grid/a.txt' overwrite into table t2;

select * from t2;

dfs -ls /user/hive/warehouse/test.db/t2;

執行命令及結果如圖3所示。

圖3 可以看到,現在表中有兩條資料,生成了資料檔案/user/hive/warehouse/test.db/t2/a.txt

編輯a.txt,使其只有一行'ccc',然後在執行下面的命令。

load data local inpath '/home/grid/a.txt' overwrite into table t2;

select * from t2;

dfs -ls /user/hive/warehouse/test.db/t2;

執行命令及結果如圖4所示。

圖4 可以看到,現在表中只有一條資料'ccc',資料檔名沒變,但其內容重新生成。

(3)刪除表

drop table t1;

drop table t2;

show tables;

dfs -ls /user/hive/warehouse/test.db;

執行命令及結果如圖5所示。

圖5 可以看到,表資料目錄已經被刪除。

對於外部表,除了刪除表只刪除元資料而保留表資料目錄外,資料載入行為與內部表相同。

2. 分割槽表

(1)load 載入資料

本地文字檔案a.txt中有一行'aaa',執行下面的命令。

create table t1 (name string) partitioned by (country string, state string);

load data local inpath '/home/grid/a.txt' into table t1 partition (country = 'us', state = 'ca');

select * from t1;

dfs -ls /user/hive/warehouse/test.db/t1/country=us/state=ca;

執行命令及結果如圖6所示。

圖6 可以看到,向表中載入了資料'aaa',生成了資料檔案/user/hive/warehouse/test.db/t1/country=us/state=ca/a.txt

(2)load overwrite 載入資料(與非分割槽表類似,實驗略)

(3)alter table add partition 載入資料

執行下面的命令。

select * from t1;

alter table t1 add partition(country = 'us', state = 'cb') location '/a';

dfs -cp /user/hive/warehouse/test.db/t1/country=us/state=ca/a.txt /a;

dfs -ls /a;

select * from t1;

dfs -rm /user/hive/warehouse/test.db/t1/country=us/state=ca/a.txt;

select * from t1;

執行命令及結果如圖7所示。

圖7 說明:表中原有一條資料'aaa'。新增乙個新分割槽,並指定位置為'/a'。把已經存在的資料檔案a.txt複製到目錄'/a'裡。此時查詢表已經有屬於不同分割槽的兩條資料。刪除country = 'us', state = 'ca'分割槽的資料檔案。此時查詢表只有屬於country = 'us', state = 'cb'分割槽的一條資料。

(4)刪除資料表

dfs -ls /user/hive/warehouse/test.db;

dfs -ls /;

drop table t1;

show tables;

dfs -ls /user/hive/warehouse/test.db;

dfs -ls /;

執行命令及結果如圖8所示。

圖8 可以看到,表資料目錄已經被刪除。

對於外部表,除了刪除表只刪除元資料而保留表資料目錄外,資料載入行為與內部表相同。

總結:

1. load與load overwrite的區別是:

load 每次執行生成新的資料檔案,檔案中是本次載入的資料。

load overwrite如表(或分割槽)的資料檔案不存在則生成,存在則重新生成資料檔案內容。

2. 內部表與外部表的區別是(無論是否分割槽):

刪除表時,內部表會刪除表的元資料和表資料目錄,外部表只會刪除元資料而保留資料目錄。

3. 分割槽表比非分割槽表多了一種alter table ... add partition的資料載入方式。

4. 對於分割槽表(無論內部還是外部),load與load overwrite會自動建立名為分割槽鍵值的目錄,而alter table ... add partition,只要用location指定資料檔案所在的目錄即可。

hive表載入資料

總結自己在hive表中常用的幾種載入資料的方式 1.load data 常用 load data inpath 集群路徑.txt load data local inpath 本地路徑 2.select 偶爾用 insert into table tablename1 select from tab...

Hive表刪除表部分資料

1 hive表刪除資料不能使用deletefrom table name 中sql語句 2 hive表刪除資料要分為不同的粒度 table partition partition內 alter table table name drop partition partiton name value i...

Hive刪除表中資料

發現hive表刪除資料不能使 用 color 不能使用 delete from table name 中sql語句 1.刪除符合條件的資料 其中 是你需要保留的資料的查詢條件。insert overwrite table t table1 select from t table1 where x i...