#在sqoop的conf下配置sqoop-env.sh
export HADOOP_COMMON_HOME=/opt/module/hadoop-3.1.3
export HADOOP_MAPRED_HOME=/opt/module/hadoop-3.1.3
export HIVE_HOME=/opt/module/hive-3.1.2
export ZOOKEEPER_HOME=/opt/module/zookeeper-3.5.7
export ZOOCFGDIR=/opt/module/zookeeper-3.5.7
export HBASE_HOME=/opt/module/hbase-2.0.5
#拷贝mysql驱动包到sqoop的lib下,再执行如下命令即可查看到mysql数据即为成功
bin/sqoop list-databases --connect jdbc:mysql://hadoop162:3306/ --username root --password aaaaaa
#mysql某一数据库下的全表导入到HDFS指定路径存储
bin/sqoop import-all-tables \
--connect jdbc:mysql://hadoop162:3306/girls \
--username root \
--password aaaaaa \
--warehouse-dir /user/girls \
--fields-terminated-by ","
#mysql某一数据库下的单表导入到HDFS指定路径存储
$ bin/sqoop import \
--connect jdbc:mysql://hadoop102:3306/myemployees \
--username root \
--password aaaaaa \
--table employees \
--target-dir /user/myemployees \
--num-mappers 4 \
--fields-terminated-by "\t"
#mysql某一数据库下的单表导入到hive
$ bin/sqoop import \
--connect jdbc:mysql://hadoop162:3306/girls \
--username root \
--password aaaaaa \
--table admin \
--num-mappers 4 \
--hive-import \
--fields-terminated-by "\t" \
--hive-overwrite \
--hive-table hivedb.admin
#hive数据导出到mysql
注意:
1、导出前要建好表
bin/sqoop export \
--connect jdbc:mysql://hadoop162:3306/test \
--username root \
--password aaaaaa \
--table admin \
--num-mappers 4 \
--export-dir /user/hive/warehouse/hivedb.db/admin \
--input-fields-terminated-by "\t"
#hdfs数据导出到mysql
注意:
1、导出前要建好表
2、如果导入到mysql出现乱码则数据后面加characterEncoding=UTF-8
bin/sqoop export \
--connect "jdbc:mysql://hadoop162:3306/test?characterEncoding=UTF-8" \
--username root \
--password aaaaaa \
--table boys \
--num-mappers 4 \
--export-dir /user/girls/boys \
--input-fields-terminated-by ","