在hive-0.8.0后引入了import/export命令。
Export命令可以導出一張表或分區的數據和元數據信息到一個輸出位置,並且導出數據可以被移動到另一個hadoop集群或hive實例,並且可以通過import命令導入數據。
當導出一個分區表,原始數據可能在hdfs的不同位置,export/import命令也支持導出分區表的不同子分區。
導出的元數據存儲在目標目錄,並且數據文件是存儲在不同的子目錄下。
Export/import命令可以獨立工作在使用存儲元數據的rdbms中。
一、語法
Export語法:
EXPORT TABLE tablename [PARTITION (part_column="value"[, ...])] TO 'export_target_path' [ FOR replication('eventid') ] |
Import語法:
IMPORT [[EXTERNAL] TABLE new_or_original_tablename [PARTITION (part_column="value"[, ...])]] FROM 'source_path' [LOCATION 'import_target_path'] |
二、使用復制:
Export/import命令當在復制環境中使用時略有不同,並且確定使用該工具在兩個數據倉庫之間使用復制。在大多數情況下,用戶不需要使用這個附加功能,除非手動引導倉庫之間的復制,這樣它可以作為一個增量復制工具。
他們使用一個特殊的表屬性“repl.last.id”在一個表或分區對象中,確保export/import工具每次復制的數據時最近更新的數據。在導出完成后,會對export的dump文件使用一個id打一個復制標簽,表示在源倉庫集成商單調遞增的。此外,為復制導出打印的標記不會導致錯誤如果試圖導出一個對象但是標記列當前不存在。
在import方面,沒有語法變化,但是import有一個一般性的標簽對於復制的dump文件,他講檢查要復制的對象是否存在,如果對象已經存在,它檢查對象的repl.last.id屬性,確定是否導入當前對象的最新數據對於目標倉庫,如果更新是最新的,那么它將復制最新的信息,如果更新已經是很舊的了對於已經存在的對象,那么更新將被忽略,並且不會產生錯誤。
對於那些使用export進行首次手動引導用例,用戶推薦使用“引導”標簽,
三、示例
1、簡單導入和導出
export table department to 'hdfs_exports_location/department'; import from 'hdfs_exports_location/department'; |
2、在import時重命名表
export table department to 'hdfs_exports_location/department'; import table imported_dept from 'hdfs_exports_location/department'; |
3、導出分區並且導入
export table employee partition (emp_country="in", emp_state="ka") to 'hdfs_exports_location/employee'; import from 'hdfs_exports_location/employee'; |
4、導出表並且導入到分區表分區
export table employee to 'hdfs_exports_location/employee'; import table employee partition (emp_country="us", emp_state="tn") from 'hdfs_exports_location/employee'; |
5、指定導入位置
export table department to 'hdfs_exports_location/department'; import table department from 'hdfs_exports_location/department' location 'import_target_location/department'; |
6、導入作為一個外部表
export table department to 'hdfs_exports_location/department'; import external table department from 'hdfs_exports_location/department'; |