Sqoop

来源:互联网 发布:mac能玩gta5 编辑:程序博客网 时间:2024/06/05 03:41

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数据库的用户密码

导入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

如果报错,可以查看MySQL是否启动
连接MySQL
mysql -h localhost -P3306 -u root –p
查看MySQL是否启动
ps aux|grep mysqld

导入到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/

0 0