目录
一.安装
1.下载地址:sqoop下载地址

2.解压
python
tar -zxvf ./sqoop-1.4.7.bin__hadoop-2.6.0.tar.gz -C ../module/
3.改名和配置归属权限
bash
#改名
mv sqoop-1.4.7.bin__hadoop-2.6.0 sqoop
#设置归属用户名和用户组(如果是root权限登入可以无需配置)
chown -R root:root sqoop
4.配置环境变量
vim /etc/profile

最后刷新配置: source /etc/profile
验证是否安装好sqoop
bash
sqoop list-databases -connect jdbc:mysql://bigdata1:3306 --username root --password 123456

二.导入
1.全量导入
一.MySQL导入HDFS
python
sqoop import #导入
--connect jdbc:mysql://bigdata1:3306/ds_db01 \ #配置连接url地址
--username root \ #用户名
--password 123456 \ #密码
--delete-target-dir \ #如果该地址存在则删除
--target-dir /mydata2 \ #hdfs目标路径
--fields-terminated-by '\t' \ #配置它的列分隔符
--table brand_info \ #mysql表名
--m 1 #配置map数为1,因为整个过程中只用了map没有reduce
查看是否导入成功

二.MySQL导入Hive
注意:
--hive-import 的配置项要在 --hive-database 之前
python
sqoop import \ #导入
--connect jdbc:mysql://bigdata1:3306/ds_db01 \ #url和mysql库名
--username root \ #用户名
--password 123456 \ #密码
--table supplier_info \ #数据库表名
--hive-import \ #导入hive
--m 1 \ #map并行度
--hive-database sqoop_db; #hive目标数据库
查看hive中是否导入成功

2.增量导入
将满足条件的数据抽取到hdfs或hive,需要用到配置参数为 --where '表达式'
一.过滤导入hdfs/hive
1.1原表数据:

1.2增量导入命令
注意: where一般位于--table
python
sqoop import -connect jdbc:mysql://bigdata1:3306/gehui \
--username root \
--password 123456 \
--where "tel=999999" \ #设置增量条件
--table user \
--target-dir /mydata4 \
--fields-terminated-by '\t' \
--m 1
1.3增量导入结果

二.导出
hdfs导出到mysql中
python
sqoop export \
--connect jdbc:mysql:///gehui:3306?characterEncoding=UTF-8 \
--username root \
--password 123456\
--table test1 \
--m 1 \
--export-dir /root/sort-result #需要导出的目录
--export-dir : 参数指定要从中导出数据的HDFS目录。