hive中導入導出的文法見:LanguageManual ImportExport,導入導出是在hive0.8.0版本之後才有的功能。
1.export
導出,将hive表中的資料導出到外部,導出的文法如下:
EXPORT TABLE tablename [PARTITION (part_column=``"value"``[, ...])]
``TO ``'export_target_path'` `[ FOR replication(``'eventid'``) ]
export_target_path指的是hdfs上的路徑
導出emp的表到hdfs檔案系統上:
export table default.emp to ‘/user/hive/user1/export/emp_exp’;
hive (default)> export table default.emp to '/user/hive/user1/export/emp_exp';
Copying data from file:/tmp/hive/491d56cd-809f-40f5-b762-98612808c96f/hive_2019-02-20_22-11-28_684_1082873653179372501-1/-local-10000/_metadata
Copying file: file:/tmp/hive/491d56cd-809f-40f5-b762-98612808c96f/hive_2019-02-20_22-11-28_684_1082873653179372501-1/-local-10000/_metadata
Copying data from hdfs://node1:8020/user/hive/warehouse/emp
Copying file: hdfs://node1:8020/user/hive/warehouse/emp/emp.txt
OK
Time taken: 1.045 seconds
export在執行的時候并沒有啟動MapReduce,其實隻是将hive表對應的檔案拷貝到hdfs指定路徑下。
hive (default)> dfs -ls /user/hive/user1/export/emp_exp;
Found 2 items
-rwxrwxr-t 3 hive hive 1567 2019-02-20 22:11 /user/hive/user1/export/emp_exp/_metadata
drwxrwxr-t - hive hive 0 2019-02-20 22:11 /user/hive/user1/export/emp_exp/data
hive (default)> dfs -ls /user/hive/user1/export/emp_exp/data/ ;
Found 1 items
-rwxrwxr-t 3 hive hive 659 2019-02-20 22:11 /user/hive/user1/export/emp_exp/data/emp.txt
通過檢視hdfs指定的路徑,可以看到export将資料檔案和中繼資料都導出來了,由此可以想到export可以用來備份hive表。
2.import
導入,将外部資料導入到hive表中。import的文法如下:
IMPORT [[EXTERNAL] TABLE new_or_original_tablename [PARTITION (part_column=``"value"``[, ...])]]
``FROM ``'source_path'
``[LOCATION ``'import_target_path'``]
首先建立一個資料庫,然後在建立的資料庫裡面建立一張emp表:
hive (default)> show databases;
OK
database_name
default
Time taken: 0.044 seconds, Fetched: 1 row(s)
hive (default)> create database test;
OK
Time taken: 0.139 seconds
hive (default)> use test;
OK
Time taken: 0.068 seconds
hive (test)> show tables;
OK
tab_name
Time taken: 0.038 seconds
hive (test)> create table test.emp like default.emp;
OK
Time taken: 0.245 seconds
hive (test)> show tables;
OK
tab_name
emp
Time taken: 0.122 seconds, Fetched: 1 row(s)
hive (test)> select * from emp;
OK
empno ename job mgr hiredate sal comm deptno
Time taken: 0.42 seconds
可以看出使用like建立的emp表中沒有資料,下面就用import來往表中導入資料:
import table test.emp from ‘/user/hive/user1/export/emp_exp’;
hive (test)> import table test.emp from '/user/hive/user1/export/emp_exp';
Copying data from hdfs://node1:8020/user/hive/user1/export/emp_exp/data
Copying file: hdfs://node1:8020/user/hive/user1/export/emp_exp/data/emp.txt
Loading data to table test.emp
OK
Time taken: 0.57 seconds
hive (test)> select * from emp;
OK
empno ename job mgr hiredate sal comm deptno
7369 SMITH CLERK 7902 1980-12-17 800.0 NULL 20
7499 ALLEN SALESMAN 7698 1981-2-20 1600.0 300.0 30
7521 WARD SALESMAN 7698 1981-2-22 1250.0 500.0 30
7566 JONES MANAGER 7839 1981-4-2 2975.0 NULL 20
7654 MARTIN SALESMAN 7698 1981-9-28 1250.0 1400.0 30
7698 BLAKE MANAGER 7839 1981-5-1 2850.0 NULL 30
7782 CLARK MANAGER 7839 1981-6-9 2450.0 NULL 10
7788 SCOTT ANALYST 7566 1987-4-19 3000.0 NULL 20
7839 KING PRESIDENT NULL 1981-11-17 5000.0 NULL 10
7844 TURNER SALESMAN 7698 1981-9-8 1500.0 0.0 30
7876 ADAMS CLERK 7788 1987-5-23 1100.0 NULL 20
7900 JAMES CLERK 7698 1981-12-3 950.0 NULL 30
7902 FORD ANALYST 7566 1981-12-3 3000.0 NULL 20
7934 MILLER CLERK 7782 1982-1-23 1300.0 NULL 10
Time taken: 0.113 seconds, Fetched: 14 row(s)
另外,export和import還可以配合分區表、外部表使用,可以用來對hive表中的資料進行備份和恢複。
更多有關大資料的内容請關注微信公衆号:大資料與人工智能初學者
掃描下面的二維碼即可關注:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TUYR2cS1WYxh2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZwpmLzITM5MDMxIjM3IjMwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)