具体语法如下
EXPORT TABLE tablename [PARTITION (part_column="value"[, ...])]
TO 'export_target_path' [ FOR replication('eventid') ]
IMPORT [[EXTERNAL] TABLE new_or_original_tablename [PARTITION (part_column="value"[, ...])]]
FROM 'source_path'
[LOCATION 'import_target_path']
- 1
- 2
- 3
- 4
- 5
- 6
例1 简单表的导入导出:
hive
use test1;
export table emp to '/user/hive/warehouse/exp_emp';
exit;
hdfs dfs -ls /user/hive/warehouse/exp_emp
# 将表emp导出到hdfs的/user/hive/warehouse/exp_emp目录中
# 导出的数据包括"_metadata"和"data"两个文件
# 将该目录 hdfs dfs -get 下来
# 将该目录 hdfs dfs -put 到另外一套hadoop集群之上
# 当然实验的话也可以直接放到一个hadoop集群的另外的一个库上
hive
use test4;
show tables;
import from '/user/hive/warehouse/exp_emp';
show tables;
select * from test1.emp;
select * from test4.emp;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
例2 简单表的导出改名导入:
hive
use test1;
export table emp to '/user/hive/warehouse/exp_emp';
exit;
hdfs dfs -ls /user/hive/warehouse/exp_emp
hive
use test4;
show tables;
import table rn_emp from '/user/hive/warehouse/exp_emp';
show tables;
select * from test1.emp;
select * from test4.emp;
select * from test4.rn_emp;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
例3 分区表导入导出:
hive
use test1;
export table emp_partition
partition (deptno="10")
to '/user/hive/warehouse/exp_emp_part';
exit;
hive
use test4;
import from '/user/hive/warehouse/exp_emp_part';
select * from test1.emp_partition where deptno=10;
select * from test4.emp_partition;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
例4 分区表导出,改名装入某个分区:
hive
use test1;
export table emp_partition
to '/user/hive/warehouse/exp_emp_part_full';
exit;
hive
use test4;
import table emp_partition_rn partition (deptno="10")
from '/user/hive/warehouse/exp_emp_part_full';
-- 这个地方做了改名和分区过滤之后的导入
select * from test1.emp_partition;
select * from test4.emp_partition_rn;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
例5 分区表导出,改名装入某个分区并指定hdfs目录:
hive
use test1;
export table emp_partition
to '/user/hive/warehouse/exp_emp_part_full';
exit;
hive
use test4;
import table emp_partition_rn2 partition (deptno="10")
from '/user/hive/warehouse/exp_emp_part_full'
location '/user/hive/warehouse/exp_emp_part_full_rn2';
-- 增加了location参数指定hdfs对应的目录
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
例6 内部表到外部表的导入导出转换:
hive
use test1;
export table emp to '/user/hive/warehouse/exp_emp_ext';
exit;
hive
use test4;
show tables;
import external table rn_emp from '/user/hive/warehouse/exp_emp_ext';
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
[TOC]
来自@若泽大数据