Hive數據導入導出

Hive的數據導入導出指的是將數據從本地或者是hdfs導入到表中,其中本地導入的話,其實是兩部操作,一是上傳到hdfs,二是在hdfs移動到倉庫位置。記住,本地上傳一定要加local

一、數據導入

1、Load模式

1)語法

hive> load data [local] inpath   文件路徑  [overwrite] into table 表名 [partition (partcol1=val1,…)];

local:表示從本地加載數據到hive表;否則從HDFS加載數據到hive表,在hdfs上是移動數據,不是複製

overwrite:表示覆蓋表中已有數據,否則表示追加,寫了它也得寫into

2、Insert

into是表示的追加,overwrite指的是覆蓋

1)insert into 表名 partition (partcol1=val1,…)  values(val1, val2)

Hive插入數據很慢,其實,hive更適合做讀操作

2) As select創建表的時候加載數據,或者從其他的表中獲得數據在  insert into select

實例1:create table if not exists student3   as select id, name from student;

實例2: insert into student   select  id,name from student_ori; 

3) 創建表時通過Location指定加載數據路徑

hive (default)> create table if not exists student5(

              id int, name string

              )    row format delimited fields terminated by '\t'    location '/user/hive/warehouse/student5';

4)   Import數據到指定Hive表中

注意:先用export導出後,再將數據導入。因爲export導出的時候有元數據,

hive (default)> import table student2 partition(month='201709') from

 '/user/hive/warehouse/export/student';

二、數據導出

1、insert 導出 (只會這種就可以)

最常用是Insert導出

寫入本地文件和hdfs只能用overwrite,不能用into

1).將查詢的結果導出到本地

hive (default)> insert overwrite local directory '/opt/module/datas/export/student'   select * from student;

2).將查詢的結果格式化導 出到本地

hive(default)>insert overwrite local directory '/opt/module/datas/export/student1'    ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'             select * from student;

3).將查詢的結果導出到HDFS上(沒有local)

hive (default)> insert overwrite directory '/user/atguigu/student2'  ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'    select * from student;

 

2、 Hadoop命令導出到本地

hive (default)> dfs -get /user/hive/warehouse/student/month=201709/000000_0

/opt/module/datas/export/student3.txt;

3、 Hive Shell 命令導出

基本語法:(hive -f/-e 執行語句或者腳本 > file)一個大於號是追加,兩個是覆蓋

$ bin/hive -e 'select * from default.student;' >   /opt/module/datas/export/student4.txt;

4、Export導出到HDFS

(defahiveult)> export table default.student to    '/user/hive/warehouse/export/student';

5、Sqoop導出

三 清除表中數據(Truncate)

注意:Truncate只能刪除管理表,不能刪除外部表中數據

hive (default)> truncate table student;

 

發佈了53 篇原創文章 · 獲贊 37 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章