Sqoop是用来实现结构型数据(如:关系型数据库RDBMS)和Hadoop之间进行数据迁移的工具。它充分利用了MapReduce的并行特点以批处理的方式加快数据的传输,
同时也借助MapReduce实现容错。一、将MySQL中的表迁移到HDFS上(RDBMS —> HDFS)
- 导入MySQL"db_sqoop.mysql_emp表"所有数据到HDFS中;
sqoop import \--connect jdbc:mysql://hadoopone:3306/db_sqoop \--username root \--password root \--table mysql_emp \--target-dir /data/hdfs_sqoop \--delete-target-dir \--num-mappers 1 \--fields-terminated-by '\t'
- 将MySQL的查询结果导入HDFS上;
sqoop import \--connect jdbc:mysql://hadoopone:3306/db_sqoop \--username root \--password root \--target-dir /data/hdfs_sqoop \--delete-target-dir \--num-mappers 1 \--fields-terminated-by '\t' \--query 'SELECT emp_no,first_name,last_name FROM mysql_emp WHERE gender="M" and $CONDITIONS;'
- 使用sqoop的关键字筛选查询后的结果导入到HDFS;
sqoop import \--connect jdbc:mysql://hadoopone:3306/db_sqoop \--username root \--password root \--table mysql_emp \--target-dir /data/hdfs_sqoop \--delete-target-dir \--num-mappers 1 \--fields-terminated-by '\t' \--where "gender='F'"
- 从MySQL导入指定’列’到HDFS上;
sqoop import \--connect jdbc:mysql://hadoopone:3306/db_sqoop \--username root \--password root \--table mysql_emp \--target-dir /data/hdfs_sqoop \--delete-target-dir \--num-mappers 1 \--fields-terminated-by '\t' \--columns emp_no,gender,birth_date
二、MySQL ——> Hive (RDBMS ——> HIVE)
- 将MySQL中的表导入Hive中;
sqoop import \--connect jdbc:mysql://hadoopone:3306/db_sqoop \--username root \--password root \--num-mappers 1 \--table mysql_emp \--hive-import \--hive-table db_hive_sqoop.hive_emp \--fields-terminated-by '\t' \--hive-overwrite \--delete-target-dir
三、MySQL –> Hbase(RDBMS ——> Hbase)
- 将MySQL中的表导入Hbase中;
sqoop import \--connect jdbc:mysql://hadoopone:3306/db_sqoop \--username root \--password root \--table book \--columns "id,name,price" \--column-family "info" \--hbase-create-table \--hbase-row-key "id" \--hbase-table "hbase_book" \--num-mappers 1 \--split-by id
四、HDFS/Hive —> MySQL (HDFS/Hive —> RDBMS)
- 在MySQL中创建对应的表,然后使用如下语句;
sqoop export \--connect jdbc:mysql://hadoopone:3306/db_sqoop \--username root \--password root \--table myqsl_dept \--num-mappers 1 \--export-dir /usr/hive/warehouse/db_hive_sqoop.db/hive_dept \--input-fields-terminated-by '\t'
五、导出MySQL表结构到hive
- 导出MySQL表结构;
sqoop create-hive-table \--connect jdbc:mysql://hadoopone:3306/db_sqoop \--username root \--password root \--table mysql_stu \--hive-database db_hive_sqoop \--hive-table hive_stu