怎么实现跨服务器自动复制sql表数据?
MySQL::MySQLGUIToolsBundl:Archiv
如何用sqoop将hive分区表信息导入到mysql命令?
问题分析:
hive中分区表其底层就是HDFS中的多个目录下的单个文件,hive导出数据本质是将HDFS中的文件导出
hive中的分区表,因为分区字段(静态分区)不在文件中,所以在sqoop导出的时候,无法将分区字段进行直接导出
思路:在hive中创建一个临时表,将分区表复制过去后分区字段转换为普通字段,然后再用sqoop将tmp表导出即实现需求
步凑如下:
文章目录
1.创建目标表(分区表)
1.1查看表结构
2.导入数据
3.查询表dept_partition
4.创建临时表tmp_dept_partition
5.查询临时表
6.查看表结构(这个时候分区表已经转换为非分区表了)
中建表dept_partition
8.使用sqoop导入到MySQL
查询验证是否成功导出
1.创建目标表(分区表)
hivegtCREATETABLE`dept_partition`(
`deptno`int,
`dname`string,
`loc`string)
PARTITIonEDBY(`month`string)rowformatdelimitedfieldsterminatedbyt
1
2
3
4
5
1
2
3
4
5
1.1查看表结构
hivegtshowcreatetabledept_partition
1
1
------------------------------------------------------
|createtab_stmt|
------------------------------------------------------
|CREATETABLE`dept_partition`(|
|`deptno`int,|
|`dname`string,|
|`loc`string)|
|PARTITIonEDBY(|
|`month`string)
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
2.导入数据
hivegtloaddatainpath/user/hive/hive_db/data/dept.txtintotabledept_partition
1
1
10tACCOUNTINGt1700
20tRESEARCHt1800
30tSALESt1900
40tOPERATIONSt1700
1
2
3
4
1
2
3
4
3.查询表dept_partition
hivegtselect*fromdept_partition
1
1
---------------------------------------------------------------------------------------------
|dept_|dept_partition.dname|dept_partition.loc|dept_|
---------------------------------------------------------------------------------------------
|10|ACCOUNTING|1700|2019-10-19|
|20|RESEARCH|1800|2019-10-19|
|30|SALES|1900|2019-10-19|
|40|OPERATIONS|1700|2019-10-19|
|10|ACCOUNTING|1700|2019-10-20|
|20|RESEARCH|1800|2019-10-20|
|30|SALES|1900|2019-10-20|
|40|OPERATIONS|1700|2019-10-20|
---------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
4.创建临时表tmp_dept_partition
hivegtcreatetabletmp_dept_partitionasselect*fromdept_partition
1
1
5.查询临时表
hivegtselect*fromtmp_dept_partition
1
1
-------------------------------------------------------------------------------------------------------------
|tmp_dept_|tmp_dept_partition.dname|tmp_dept_partition.loc|tmp_dept_|
-------------------------------------------------------------------------------------------------------------
|10|ACCOUNTING|1700|2019-10-19|
|20|RESEARCH|1800|2019-10-19|
|30|SALES|1900|2019-10-19|
|40|OPERATIONS|1700|2019-10-19|
|10|ACCOUNTING|1700|2019-10-20|
|20|RESEARCH|1800|2019-10-20|
|30|SALES|1900|2019-10-20|
|40|OPERATIONS|1700|2019-10-20|
-------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
6.查看表结构(这个时候分区表已经转换为非分区表了)
hivegtshowcreatetabletmp_dept_partition
1
1
------------------------------------------------------
|createtab_stmt|
------------------------------------------------------
|CREATETABLE`tmp_dept_partition`(|
|`deptno`int,|
|`dname`string,|
|`loc`string,|
|`month`string)
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
中建表dept_partition
mysqlgtdroptableifexistsdept_partition
createtabledept_partition(
`deptno`int,
`dname`varchar(20),
`loc`varchar(20),
`month`varchar(50))
1
2
3
4
5
6
1
2
3
4
5
6
8.使用sqoop导入到MySQL
bin/sqoopexport
--connectjdbc:mysql://hadoop01:3306/partitionTb
--usernameroot
--password123456
--tabledept_partition
--num-mappers1
--export-dir/user/hive/warehouse/hive_db.db/tmp_dept_partition
--input-fields-terminated-by