imp exp to in mysql in hadoop

Franklyn 2015-06-29

前置条件

已经成功安装配置Hadoop和Mysql数据库服务器,如果将数据导入或从Hbase导出,还应该已经成功安装配置Hbase。

下载sqoop和Mysql的JDBC驱动

sqoop-1.2.0-CDH3B4.tar.gz :http://archive.cloudera.com/cdh/3/sqoop-1.2.0-CDH3B4.tar.gz

mysql-connector-java-5.1.28

安装sqoop

[hadoop@appserver ~]$ tar -zxvf sqoop-1.2.0-CDH3B4.tar.gz

配置环境变量

imp exp to in mysql  in hadoop

拷贝Hadoop核心包和MYSQL驱动包到sqooplib目录

[hadoop@appserver ~]$ cp hadoop-1.1.2/hadoop-core-1.1.2.jar sqoop-1.2.0-CDH3B4/lib/

[hadoop@appserver ~]$ cp  mysql-connector-java-5.1.28-bin.jar sqoop-1.2.0-CDH3B4/lib/

imp exp to in mysql  in hadoop

配置sqoop-1.2.0-CDH3B4/bin/configure-sqoop文件

注释掉hbase和zookeeper检查(除非准备使用HABASE等HADOOP组件)

imp exp to in mysql  in hadoop

启动hadoop集群

imp exp to in mysql  in hadoop

启动mysql

imp exp to in mysql  in hadoop

创建sqoop用户

imp exp to in mysql  in hadoop

建立sqoop库,test表,并构造测试数据

imp exp to in mysql  in hadoop

测试sqoop连接

[hadoop@appserver ~]$ sqoop list-databases --connect jdbc:mysql://10.120.10.11:3306/ --username sqoop --password sqoop

imp exp to in mysql  in hadoop

列出mysql中所有数据库的名称

从mysql导入到hdfs中

sqoop ##sqoop命令

import ##表示导入

--connect jdbc:mysql://ip:3306/sqoop ##告诉jdbc,连接mysql的url

--username sqoop ##连接mysql的用户名

--password sqoop ##连接mysql的密码

--table test ##从mysql导出的表名称

--fields-terminated-by '\t' ##指定输出文件中的行的字段分隔符

-m 1 ##复制过程使用1个map作业

[hadoop@appserver ~]$ sqoop import --connect jdbc:mysql://10.120.10.11:3306/sqoop --username sqoop --password sqoop --table test --fields-terminated-by ':' -m 1

imp exp to in mysql  in hadoop

Hadoop中查看导入结果

imp exp to in mysql  in hadoop

从hdfs导出到mysql中

sqoop

export ##表示数据从hive复制到mysql中

--connect jdbc:mysql://ip:3306/sqoop

--username sqoop

--password sqoop

--table test ##mysql中的表,即将被导入的表名称

--export-dir '/user/root/aa/part-m-00000' ##hive中被导出的文件

--fields-terminated-by '\t' ##hive中被导出的文件字段的分隔符

[hadoop@appserver ~]$ sqoop export --connect jdbc:mysql://10.120.10.11:3306/sqoop --username sqoop --password sqoop --table test --export-dir '/user/hadoop/test/part-m-00000' --fields-terminated-by ':' -m 1

imp exp to in mysql  in hadoop

Mysql中查看导出结果

imp exp to in mysql  in hadoop

从Mysql导入到Hbase中

参数说明:

Ø hbase_tablename指定要导成hbase的表名

Ø key_col_name指定mysql数据库表中哪一列作为hbase新表的rowkey

Ø col_fam_name是除rowkey之外的所有列的列族名

[hadoop@appserver ~]$ sqoop import --connect jdbc:mysql://10.120.10.11:3306/sqoop --username sqoop --password sqoop --table test --hbase-create-table --hbase-table mysql_sqoop_test --column-family info --hbase-row-key id -m 1

imp exp to in mysql  in hadoop

imp exp to in mysql  in hadoop

在Hbase中查看结果

imp exp to in mysql  in hadoop

相关推荐