Hive 資料匯入的幾種方式

2021-10-11 01:27:19 字數 2292 閱讀 4664

幾種基本的匯入資料方式:

1 建表的時候指定location 結構化資料的位置資料夾 外部表

例:①新建user1.txt 檔案 將該檔案上傳到hdfs 的/data/test/目錄下

②建表create external table tb_user2(

id int,

name string

)row format delimited fields terminated by 『\t』

location 『/data/test/』;

③此時建立外部表時直接指定資料的位置,檢視表時資料直接就在表中了。

2 可以直接將結構化資料put到hdfs中表的目錄下(建表時的位置,忘了可以通過 desc formatted 表名 來查)

如果本身就是hdfs中的資料 hdfs dfs -mv 移動檔案

例:①本地建立乙個檔案寫入結構化資料 vi tb_user1.txt

②隨便建立乙個表

create external table tb_user2(

id int,

name string

)row format delimited fields terminated by 『\t』;

③此時表在hdfs中位置為:/user/hive/warehouse/tb_user2

④將檔案tb_user1.txt 上傳到該路徑下:

hdfs dfs -put ./tb_user1.txt /user/hive/warehouse/tb_user2/

⑤此時select *from tb_user2 會發現tb_user1.txt中的資料已經進入到表中了

⑥再傳一次資料(不會衝突,此時表名的目錄下會有兩個檔案。查詢表資料發現資料又多一遍)

hdfs dfs -put ./tb_user1.txt /user/hive/warehouse/tb_user2/tb_user2.txt

3 使用load語法

①本地建立乙個檔案寫入結構化資料 vi tb_user2.txt

在beeline視窗中執行命令:

load data local inpath 「/tb_user2.txt」 into table tb_user2 ; --linux本地資料load,底層就是put,

②在本地寫乙個 vi tb_user3.txt 上傳到hdfs的根目錄下(hdfs dfs -put ./tb_user3.txt /),beeline上執行以下命令

隨著load 操作原來路徑下的檔案不在了!!!

load data inpath 「/tb_user3.txt」 into table tb_user2; – 底層move

– 追加資料

③ 覆蓋資料

load data inpath 「/tb_user3.txt」 overwrite into table tb_user2 ; – 底層move

4.通過查詢新增資料(將從乙個表查詢出來的資料新增到另一表中)

① insert 直接向表中插入資料,這種方式不建議使用,因為每次插入時會在hdfs中生成小檔案,只有這些檔案合併時資料才會

真正的被刪除掉。

例:insert into tb_user values(1,『zs』),(2,『ls』) ; – 生成小檔案

② 增量匯入,表中原有資料不變,新插入的資料增加在後面。

insert into tb_user1

select id , name from tb_log ;

– 注意 查詢得到的結果欄位的 個數 屬性及其資料型別要與被插入表中的一致

③全量匯入,直接新建乙個表把查詢到的資料加入到表中

create table tb_log_res

asselect id , name from tb_user1 ;

5.import匯入(只能匯入export形式匯出的資料)指定export的資料

例:將tb_log_res表中的資料匯出到hdfs的』/user/hive/warehouse/export/tb_log_res』路徑下。

export table tb_log_res to 『/user/hive/warehouse/export/tb_log_res』;

新建tb_log_res2表(表自動建出來了,不需要額外建表),將hdfs的'/user/hive/warehouse/export/tb_log_res'路徑下的資料匯入。

匯出後hdfs資料依舊存在;

import table tb_log_res2 from '/user/hive/warehouse/export/tb_log_res';

資料匯入hive的幾種方式

可以通過多種方式將資料匯入hive表 1.通過外部表匯入 使用者在hive上建external表,建表的同時指定hdfs路徑,在資料拷貝到指定hdfs路徑的同時,也同時完成資料插入external表。例如 編輯檔案test.txt cat test.txt 1 hello 2 world 3 tes...

hive表匯入資料的幾種方式

以前學習hive的時候這些知識點應該是涉及到的,但是時間久了沒有用就會忘記,今天借這個機會來複習一下相關知識。下面總結一下hive表匯入資料的四種方式 1 從本地檔案系統中匯入資料到hive表 2 從hdfs上匯入資料到hive表 3 從別的表中查詢出相應的資料並匯入到hive表中 4 在建立表的時...

Hive幾種資料匯入匯出方式

匯入 hive幾種資料匯入方式 匯出 1.拷貝檔案 如果資料檔案恰好是使用者需要的格式,那麼只需要拷貝檔案或資料夾就可以。hadoop fs cp source path target path 2.匯出到本地檔案系統 不能使用insert into local directory來匯出資料,會報錯...