1. 程式人生 > >Hive資料的匯入和匯出

Hive資料的匯入和匯出

1.Load files into tables  

格式:

  1. load data [local] inpath filePath[overwrite]into table tablename [partition](partcol1=val1,partcol2 =val2)
  • filePath原始檔案儲存的位置 
    • 本地local
    • hdfs 
  • 對錶的資料操作
    •  覆蓋 overwrite
    •  追加    
  • 分割槽表載入,特殊性
    • partition()
 載入資料的幾種方式:
1) 載入本地檔案到hive表中
  1. load data local inpath '/opt/datas/emp.txt'into
    table default.emp;
2)載入 hdfs 檔案到hive中
  1. load data '/user/huangxgc/hive/datas/emp.txt' into table default.emp;
3) 載入資料覆蓋表中已有的資料
  1. load data '/user/huangxgc/hive/datas/emp.txt overwrite into table default.emp;
4) 建立表時通過insert 載入
  1. create table  default.emp like emp 
  2. insert intoselect*from emp;
5) 建立表的時候通過location指定載入

2.資料的匯出 insert

  • 從hive將資料覆蓋匯出到本地(檔案未進行格式化)
  1. insert overwrite local directory '/opt/datas/hive_exp_emp'
  2. select*fromdefault.emp;
  • 匯出的檔案時進行格式化
  1. insert overwrite local directory '/opt/datas/hive_exp_emp'
  2. row format delimited fields terminated by '\t' # 行內個欄位採用製表符進行分割
  3. collection items terminated by '\n'
                # 每行採用換行進行分割
  4. select*fromdefault.emp;
  • 用hive指令碼的-e匯出檔案,檔案已格式化
  1. bin/hive -e "select *from default.emp;">/opt/datas/exp_res.txt
兩個專用匯出匯入
  • Export 匯出 :將hive中的資料匯出到hdfs
語法:
  1. EXPORT TABLE TABLENAME [PARTITION(part_column="value"[,....])]
  2. TO 'exportt_target_path'
export_target_path:指的是hdfs的路徑
  • IMPORT 匯入
語法:
  1. IMPORT [EXTERNAL] TABLE new_or_original_tablename [PARTITION(part_column="value"[....])]
  2. From'source_path'
  3. [LOCATION 'import_target_path']
import_target_path:指的是hdfs的路徑