HIVE資料的匯入與匯出詳解
阿新 • • 發佈:2018-12-09
1.匯入
load data [local] inpath '' [overwrite] into database.table [partition(partcol=val)]
原始檔案在linux本地 加上local 如果原始資料檔案在hdfs 不用local
如果是覆蓋資料加上overwrite 如果是追加 不要overwrite
如果是分割槽表加上partition,不是就不用了。
2.建立表通過insert 載入
create table db_hive.dept_copy like dept; insert into table db_hive.dept_copy select * from dept ;
3.建立表時通過location指定檔案路徑
2.匯出
1.通過insert [overwrite] [local] directory
匯出資料到本地檔案 會自動建立目錄啦 注意這裡的目錄是一個資料夾
insert overwrite local directory '/opt/datas/hive_exp_dept'
select * from db_hive.dept;
檢視匯出的資料
發現數據不太好看
格式化一下
insert overwrite local directory '/opt/datas/hive_exp_dept' row format delimited fields terminated by '\t' collection items terminated by '\n' select * from db_hive.dept;
再次檢視
匯出資料到hdfs 當然就是不要local啦
insert overwrite directory '/user/hive/warehouse/db_hive.db/dept_exp_hdfs'
select * from db_hive.dept;
50070頁面檢視
檢視
將檔案拿到本地
bin/hdfs dfs -get /user/hive/warehouse/db_hive.db/dept_exp_hdfs/0* /root/hive_datas/
2.重定向
將查詢結果重定向到一個檔案 這裡是一個檔案 上面有資料而不是資料夾
bin/hive -e "select * from db_hive.emp;" > /opt/datas/emp_exp_dept ;