1.官网:
http://sqoop.apache.org/
2.sqoop,datax:同类产品
3.版本:两个版本完全不兼容,sqoop1用的最多
sqoop1:1.4x
sqoop2:1.99x
4.安装步骤
(1)解压
(2)配置环境变量
export SQOOP_HOME=/xx/sqoop.xx
source /etc/profile
(3)添加数据库驱动包
cp mysql-connect-…jar /sqoop-install-path/lib
(4)重命名配置文件
mv sqoop-evn-template.sh sqoop-env.sh
(5)修改配置
…/bin/configure-sqoop
vi configure-sqoop
去掉未安装的服务
(HCatalog,Accumulo)
就是编辑文件,#HCatalog… (注释掉这两个服务,它们在文件的最后几行,每个服务都只有4行)
接下来,把环境配置一下
vi sqoop-site.xml
环境变量加5个
EXPORT HADOOP_COMMON_HOME=/opt/hadoop-2.6.0
EXPORT HADOOP_MAPRED_HOME=/opt/hadoop-2.6.0
hive 安装目录
hbase 安装目录
ZOOKEEPER(zookeeper的安装目录)
(6)测试
sqoop version
sqoop list-database -connect jdbc:mysql://node3(可以用安装节点的ip地址):3306/ -username root -password 123456
mysql数据导入到hive的代码
//下面能直接运行
sqoop import
–connect
jdbc:mysql://192.168.8.10:3306/student \ //student(数据库名)
–username
root
–password
123456
–table
student
–m
1 \ //一个map,生成的文件只有2个,一个success,一个part-0-00000
–target-dir
/sqoop/employee \ //数据导过来放在hdfs的哪个目录下
–fields-terminated-by
‘\t’
sqoop import
–connect
jdbc:mysql://192.168.8.10:3306/student \ //student(数据库名)
–username
root
–password
123456
–table
student
–m
2 \ //如果为2,就要添加分隔字段,尽量使用int类型,数据分布均匀
–split-by
id
–target-dir
/sqoop/employee \ //数据导过来放在hdfs的哪个目录下
–fields-terminated-by
‘\t’
select * from student where id> and id<
hive数据导入到hdfs的代码
sqoop import
–connect
jdbc:mysql://192.168.8.10:3306/student \ //如果报错jdbc拒绝连接,那么ip地址可能错了
–username
root
–password
123456
–table \ //这一行可以写:–query ‘select id,age from student where $CONDITINS LIMIT 99’
score
–fields-terminated-by
“\t”
–lines-terminated-by
“\n”
–hive-import
–target-dir
/user/hive/warehouse/employee
–hive-table
employee
可以写成配置文件
import
–connect
jdbc:mysql://192.168.8.10:3306/student //如果报错jdbc拒绝连接,那么ip地址可能错了
–username
root
–password
123456
–table //这一行可以写:–query ‘select id,age from student where $CONDITINS LIMIT 99’
score
–fields-terminated-by
“\t”
–lines-terminated-by
“\n”
–hive-import
–target-dir
/user/hive/warehouse/employee //写成配置文件时,这个文件必须不能存在
–hive-table
employee
找一个文件夹
vi mysqlToHdfs.conf //自己命名
把上面代码放进去,退出,保存
//执行上面的命令,必须启动yarn,端口为:
node1:8088 或者 192.168.8.20(node1的ip地址)
sqoop --options-file sqoop1 //sqoop1:sqoop写的配置文件名
sqoop --options-file mysqlToHdfs.conf
mysql导入hbase //如果报错,zookeeper可能没有启动 zkServer.sh start,start-hbase.sh
import
–connect
jdbc:mysql://192.168.8.10:3306/student
–username
root
–password
123456
–table
student
–hbase-table
student
–hbase-create-table
–hbase-row-key
id
//–split-by //这里为-m为1,不用指定
//date
-m
1
–column-family
info
start-hbase.sh
starting…,logging to /usr/local/…/habse-0.98-hadoop2/bin…/logs/hbase-root-master-node1.out
tail -f /usr/local/…/habse-0.98-hadoop2/bin…/logs/hbase-root-master-node1.log //看日志
出现:Master has completed initialization 就说明成功了
也可以去:node1:60010看网页
加入此时,上面导入命令中的student表存在了,现在要删除它
hbase shell
disable ‘student’
drop ‘student’
现在就可以了,重新执行导入命令
hdfs导入mysql
export
–connect
jdbc:mysql://192.168.8.10:3306/student
–username
root
–password
123456
-m
1
–columns
id,name //数据和字段必须匹配
–export-dir
/user/hive/warehouse/employee
–fields-terminated-by
‘\t’
–table
employee //如果表存在,且其中有数据,则不成功,如果表存在,但进行了数据清空,则成功, //或者提前认为在mysql中把表创建好