sqoop 使用场景
import :全部导入
1 $ bin/sqoop import \ 2 --connect jdbc:mysql://hadoop102:3306/company \ 3 --username root \ 4 --password 000000 \ 5 --table staff \ 6 --target-dir /user/company \ 7 --delete-target-dir \ 8 --num-mappers 1 \ 9 --fields-terminated-by "\t"
import :查询导入
1 $ bin/sqoop import \ 2 --connect jdbc:mysql://hadoop102:3306/company \ 3 --username root \ 4 --password 000000 \ 5 --target-dir /user/company \ 6 --delete-target-dir \ 7 --num-mappers 1 \ 8 --fields-terminated-by "\t" \ 9 --query 'select name,sex from staff where id <=1 and $CONDITIONS;' 10 11 说明: 12 1.must contain '$CONDITIONS' in WHERE clause. 13 2.如果query后使用的是双引号,则$CONDITIONS前必须加转移符,防止shell识别为自己的变量。
import:导入指定列
1 $ bin/sqoop import \ 2 --connect jdbc:mysql://hadoop102:3306/company \ 3 --username root \ 4 --password 000000 \ 5 --target-dir /user/company \ 6 --delete-target-dir \ 7 --num-mappers 1 \ 8 --fields-terminated-by "\t" \ 9 --columns id,sex \ 10 --table staff 11 12 提示:columns中如果涉及到多列,用逗号分隔,分隔时不要添加空格
import:使用sqoop关键字筛选查询导入数据
1 $ bin/sqoop import \ 2 --connect jdbc:mysql://hadoop102:3306/company \ 3 --username root \ 4 --password 000000 \ 5 --target-dir /user/company \ 6 --delete-target-dir \ 7 --num-mappers 1 \ 8 --fields-terminated-by "\t" \ 9 --table staff \ 10 --where "id=1"
RDBMS 到Hive
1 $ bin/sqoop import \ 2 --connect jdbc:mysql://hadoop102:3306/company \ 3 --username root \ 4 --password 000000 \ 5 --table staff \ 6 --num-mappers 1 \ 7 --hive-import \ 8 --fields-terminated-by "\t" \ 9 --hive-overwrite \ 10 --hive-table staff_hive 11 提示:该过程分为两步,第一步将数据导入到HDFS,第二步将导入到HDFS的数据迁移到Hive仓库,第一步默认的临时目录是/user/atguigu/表名
RDBMS到Hbase
1 $ bin/sqoop import \ 2 --connect jdbc:mysql://hadoop102:3306/company \ 3 --username root \ 4 --password 000000 \ 5 --table staff \ 6 --columns "id,name,sex" \ 7 --column-family "info" \ 8 --hbase-create-table \ 9 --hbase-row-key "id" \ 10 --hbase-table "hbase_company" \ 11 --num-mappers 1 \ 12 --split-by id 13 提示:sqoop1.4.6只支持HBase1.0.1之前的版本的自动创建HBase表的功能
hive/hdfs 到RDBMS
1 $ bin/sqoop export \ 2 --connect jdbc:mysql://hadoop102:3306/company \ 3 --username root \ 4 --password 000000 \ 5 --table staff \ 6 --num-mappers 1 \ 7 --export-dir /user/hive/warehouse/staff_hive \ 8 --input-fields-terminated-by "\t"