天天看點

Hive-Import/Export(導入導出)

1 官方位址

  • Hive 0.8.0 之後引入了EXPORT and IMPORT 指令。
    1. EXPORT指令将表或分區的資料連同中繼資料一起導出到指定的輸出位置(HDFS上)。然後可以将此輸出位置移至不同的Hadoop或Hive執行個體,并使用IMPORT指令進行導入操作。
    2. 導出分區表時,原始資料可能位于不同的HDFS位置。還支援導出/導入分區子集的功能。
    3. 導出的中繼資料存儲在目标目錄中,資料檔案存儲在子目錄中。
    4. EXPORT和IMPORT指令獨立于所使用的源和目标Metastore DBMS工作;例如,它們可以在Derby和MySQL資料庫之間使用。

2 EXPORT

  • 官方文法
EXPORT TABLE tablename [PARTITION (part_column="value"[, ...])]
  TO 'export_target_path' [ FOR replication('eventid') ]
           
  • 将資料導出到HDFS
export table join_a to '/home/hadoop/data';
Copying data from file:/tmp/hadoop/b96064b0-a--a110-fc9d09d7c00b/hive_2018--_08--_501_1397817648848080864-/-local-/_metadata
Copying file: file:/tmp/hadoop/b96064b0-a--a110-fc9d09d7c00b/hive_2018--_08--_501_1397817648848080864-/-local-/_metadata
Copying data from hdfs://192.168.137.200:9000/user/hive/warehouse/join_a
Copying file: hdfs://192.168.137.200:9000/user/hive/warehouse/join_a/join_a.txt
OK
           
  • 檢視HDFS上的資料
[[email protected] export]$ hdfs dfs -ls /home/hadoop/data
Found  items
-rwxr-xr-x    hadoop supergroup        -- : /home/hadoop/data/_metadata
drwxr-xr-x   - hadoop supergroup           -- : /home/hadoop/data/data
           

3 IMPORT

  • 官方文法
IMPORT [[EXTERNAL] TABLE new_or_original_tablename [PARTITION (part_column="value"[, ...])]]
  FROM 'source_path'
  [LOCATION 'import_target_path']
           
  • 導入資料
hive> import from '/home/hadoop/data';
Copying data from hdfs://192.168.137.200:9000/home/hadoop/data/data
Copying file: hdfs://192.168.137.200:9000/home/hadoop/data/data/join_a.txt
Loading data to table hive1.join_a
OK
Time taken:  seconds
           
  • 查詢
hive> show tables;
OK
join_a

hive> select * from join_a;
OK
       zhangsan
       lisi
       wangwu1
           

4 其他例子

  • Rename table on import:(重命名表名)
export table department to 'hdfs_exports_location/department';
import table imported_dept from 'hdfs_exports_location/department';
           
  • Export partition and import:(導出加上分區)
export table employee partition (emp_country="in", emp_state="ka") to 'hdfs_exports_location/employee';
import from 'hdfs_exports_location/employee';
           
  • Export table and import partition:(導入加上分區)
export table employee to 'hdfs_exports_location/employee';
import table employee partition (emp_country="us", emp_state="tn") from 'hdfs_exports_location/employee';
           
  • Specify the import location:(指定位址)
export table department to 'hdfs_exports_location/department';
import table department from 'hdfs_exports_location/department' 
       location 'import_target_location/department';
Import as an external table:
export table department to 'hdfs_exports_location/department';
import external table department from 'hdfs_exports_location/department';
           

繼續閱讀