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表中的数据进行备份和恢复。
更多有关大数据的内容请关注微信公众号:大数据与人工智能初学者
扫描下面的二维码即可关注: