一,下载sqoop和mysql连接驱动:mysql-connector-java-5.1.40-bin.jar 二,配置 1,在/etc/profile中添加sqoop的安装路径。 2,将数据库连接驱动拷贝到sqoop目录下的lib中 sqoop安装完成! 三,配置mysql远程连接 在mysql命令窗口执行下面的语句 .:所有库下的所有表 %:任何IP地址或主机都可以连接)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION; FLUSH PRIVILEGES四,使用 1,数据库中的数据导入到HDFS上
sqoop import --connect jdbc:mysql://192.168.1.10:3306/itcast --username root --password 123 --table trade_detail --columns 'id, account, income, expenses'2,指定输出路径、指定数据分隔符
sqoop import --connect jdbc:mysql://192.168.1.10:3306/itcast --username root --password 123 --table trade_detail --target-dir '/sqoop/td' --fields-terminated-by '\t'3,指定Map数量 -m
sqoop import --connect jdbc:mysql://192.168.1.10:3306/itcast --username root --password 123 --table trade_detail --target-dir '/sqoop/td1' --fields-terminated-by '\t' -m 24,增加where条件, 注意:条件必须用引号引起来
sqoop import --connect jdbc:mysql://192.168.1.10:3306/itcast --username root --password 123 --table trade_detail --where 'id>3' --target-dir '/sqoop/td2'5,增加query语句(使用 \ 将语句换行)
sqoop import --connect jdbc:mysql://192.168.1.10:3306/itcast --username root --password 123 \ --query 'SELECT * FROM trade_detail where id > 2 AND $CONDITIONS' --split-by trade_detail.id --target-dir '/sqoop/td3'注意:如果使用–query这个命令的时候,需要注意的是where后面的参数,AND $CONDITIONS这个参数必须加上 单引号与双引号的区别,如果–query后面使用的是双引号,那么需要$CONDITIONS前加上\即\$CONDITIONS 如果设置map数量为1个时即-m 1,不用加上–split-by ${tablename.column},否则需要加上 6,将HDFS上的数据导出到数据库中
sqoop export --connect jdbc:mysql://192.168.8.120:3306/itcast --username root --password 123 --export-dir '/td3' --table td_bak -m 1 --fields-termianted-by '\t'