本文最后更新于 2021-08-05 11:42:59
Sqoop
sqoop目的是完成从关系型数据库导入数据或者是导出数据到关系型数据库
核心是将命名转换为MR,但是没有Reduce阶段
官方文档:https://sqoop.apache.org/docs/1.4.7/SqoopUserGuide.html
安装sqoop
- 下载sqoop编译好的版本
- 解压
- 在sqoop-env.sh或/etc/profile中配置 HADOOP_HOME,HIVE_HOME,HBASE_HOME,ZOOKEEPER_HOME
- 添加数据库驱动,例如mysql-connector到sqoop/lib中
- 测试连接
bin/sqoop list-databases --connect jdbc:mysql://node1:3306/ --username root --password 123456
- 报错解决 更换commons-lang3-3.4为commons-lang-2.6

导入数据到hadoop
导入数据到hdfs
1 2 3 4 5 6 7 8 9 10 11 12
| bin/sqoop import \ --connect jdbc:mysql://node1:3306/mydb \ --username root \ --password 123456 \ --table t_emp \ --target-dir /t_emp \ --delete-target-dir \ --fields-terminated-by "\t" \ --num-mappers 2 \ --split-by id \ --columns age,name \ --where 'id >= 5 and id <= 10'
|
1 2 3 4 5 6 7 8 9 10
| bin/sqoop import \ --connect jdbc:mysql://node1:3306/mydb \ --username root \ --password 123456 \ --query "select * from t_emp where \$CONDITIONS and id >=2" \ --target-dir /t_emp \ --delete-target-dir \ --fields-terminated-by "\t" \ --num-mappers 2 \ --split-by id
|
导入到hive
该过程分为两步,第一步将数据导入到HDFS,第二步将导入到HDFS的数据迁移到Hive仓库
1 2 3 4 5 6 7 8 9 10
| bin/sqoop import \ --connect jdbc:mysql://node1:3306/mydb \ --username root \ --password 123456 \ --table t_emp \ --num-mappers 1 \ --hive-import \ --fields-terminated-by "\t" \ --hive-overwrite \ --hive-table t_emp
|
导入到hbase
1 2 3 4 5 6 7 8 9 10
| bin/sqoop import \ --connect jdbc:mysql://node1:3306/mydb \ --username root \ --password 123456 \ --table t_emp \ --hbase-create-table \ --hbase-table "t_emp" \ --hbase-row-key "id" \ --column-family "info" \ --num-mappers 1
|
导出到关系型数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| bin/sqoop export \ --connect 'jdbc:mysql://node1:3306/mydb?useUnicode=true&characterEncoding=utf-8' \ --username root \ --password 123456 \ --table t_emp2 \ --num-mappers 1 \ --export-dir /t_emp \ --update-key id \ --update-mode allowinsert \ --input-fields-terminated-by "\t"
|