您的位置:首页 > 运维架构

Sqoop

2016-04-28 16:03 176 查看
SQOOP是用于对数据进行导入导出的。

(1)把MySQL、Oracle等数据库中的数据导入到HDFS、Hive、HBase中

(2)把HDFS、Hive、HBase中的数据导出到MySQL、Oracle等数据库中

一、Sqoop在Hadoop(Hive)与传统的数据库(mysql)间进行数据的传递

1)列出mysql数据库中的所有数据库

sqoop list-databases --connect jdbc:mysql://localhost:3306/ -username root -password 123456


2)连接mysql并列出test数据库中的表

sqoop list-tables –connect jdbc:mysql://localhost:3306/test –username root –password 123456

命令中的test为mysql数据库中的test数据库名称 username password分别为mysql数据库的用户密码

3)导入hive

sqoop import -connect jdbc:mysql://ip:3306/covers -username root -password 123456 -table sys_api_info_value -target-dir /user/hive/warehouse/covers -hive-import

4)如果报错,可以查看MySQL是否启动

连接MySQL

mysql -h localhost -P3306 -u root –p

查看MySQL是否启动

ps aux|grep mysqld

5)导入到hive指定的数据库中(不可以使用127.0.0.1)

sqoop import -connect jdbc:mysql://ip:3306/covers -username root -password 123456 -table sys_auth_info –hive-import –hive-table covers.sys_auth_info

二、导入到hdfs中:

sqoop import -connect jdbc:mysql://ip:3306/covers -username root -password 123456 -table sys_auth_info -target-dir ‘/user/hive/warehouse/my1’ -m 1

hdfs导出到MySQL,需要库里有对应的表

create table sys_auth_info(id int(11) primary key, c_user_id int(11),c_data datetime, m_user_id int(11), m_date datetime,name varchar(100),identification varchar(100),type int(1),remark text,url varchar(255),status int(1),sort_num int(11),level int(1),parent_id int(11),role_type int(1));

sqoop export –connect jdbc:mysql://ip:3306/test –username root –password 123456 –export-dir ‘/user/hive/warehouse/my1’ –table sys_auth_info -m 1 –fields-terminated-by ‘,’

导出的时候,要使用ip,不能使用localhost

java.io.IOException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure


sqoop export --connect jdbc:mysql://ip:3306/nginx -username root -password 123456 --export-dir /letv/bigdata/spark-2.0.0/spark-warehouse/nginx.db/lunbo --table lunbo


不指定时路径在默认或者hadoop fs -ls /user/root/

导入所有的表

sqoop import-all-tables -connect jdbc:mysql://ip:3306/covers -username root -password 123456 -hive-import

sqoop 搭建

同时也是解决这个的方案

java.lang.ClassNotFoundException: org.apache.hadoop.mapreduce.Job


export HADOOP_MAPRED_HOME=${HADOOP_HOME}
export HADOOP_COMMON_HOME=${HADOOP_HOME}
export HADOOP_HDFS_HOME=${HADOOP_HOME}


参考:

sqoop搭建

http://www.cnblogs.com/gw811/p/4630774.html

sqoop教程:

http://blog.csdn.net/myrainblues/article/details/43673129

http://www.cnblogs.com/edisonchou/p/4440216.html

http://shiyanjun.cn/archives/624.html

http://blog.csdn.net/tswisdom/article/details/41458951

hive到mysql

http://blog.csdn.net/jiedushi/article/details/6663177

http://blog.163.com/zhangjie_0303/blog/static/99082706201402231422375/

这是印象笔记中的,还没测试

1.把数据从mysql导入到hdfs(默认是/user/)中

sqoop import –connect jdbc:mysql://hadoop0:3306/hive –username root –password admin –table TBLS –fields-terminated-by ‘\t’ –null-string ‘**’ -m 1 –append –hive-import

sqoop import –connect jdbc:mysql://hadoop0:3306/hive –username root –password admin –table TBLS –fields-terminated-by ‘\t’ –null-string ‘**’ -m 1 –append –hive-import –check-column ‘TBL_ID’ –incremental append –last-value 6

2.把数据从hdfs导出到mysql中

sqoop export –connect jdbc:mysql://hadoop0:3306/hive –username root –password admin –table ids –fields-terminated-by ‘\t’ –export-dir ‘/ids’

3.设置为作业,运行作业

sqoop job –create myjob – import –connect jdbc:mysql://hadoop0:3306/hive –username root –password admin –table TBLS –fields-terminated-by ‘\t’ –null-string ‘**’ -m 1 –append –hive-import

sqoop job –exec myjob

如果想让其保存密码,直接运行,则设置sqoop-site.xml中 把sqoop.metastore.client.record.password的注释取消

4. 导入导出的事务是以Mapper任务为单位。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sqoop