用Sqoop进行MySQL和HDFS系统间的数据互导(4)

三、从hdfs导出到MySQL
 
清空表
 mysql> delete from test;
Query OK, 2 rows affected (0.00 sec)

mysql> select * from test;
Empty set (0.00 sec)
 
导出
 [zhouhh@Hadoop48 ~]$ sqoop-export --connect jdbc:mysql://192.168.10.48:3306/toplists --username root --table test --export-dir test
Note: /tmp/sqoop-zhouhh/compile/7adaaa7ffe5f49ed9d794b1be8a9a983/test.Java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
 
导出时,–connect,–table, –export-dir是必须设置的。其中toplists是库名,–table是该库下的表名。 –export-dir是要导出的HDFS平面文件位置。如果不是绝对路径,指/user/username/datadir
 
检查mysql表
 mysql> select * from test;
+----+--------+---------+------------+---------+---------+
| id | name | price | cdate | version | comment |
+----+--------+---------+------------+---------+---------+
| 1 | iphone | 3900.00 | 2012-07-18 | 1 | 8g |
| 2 | ipad | 3200.00 | 2012-07-16 | 2 | 16g |
+----+--------+---------+------------+---------+---------+
2 rows in set (0.00 sec)
 
可见导出成功。
 
 
 
四、不执行mapreduce,但生成导入代码
 [zhouhh@Hadoop48 ~]$ sqoop codegen --connect jdbc:mysql://192.168.10.48:3306/toplists --username root --table test --class-name Mycodegen
Note: /tmp/sqoop-zhouhh/compile/104b871487669b89dcd5b9b2c61f905f/Mycodegen.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

[zhouhh@Hadoop48 ~]$ sqoop help codegen
usage: sqoop codegen [GENERIC-ARGS] [TOOL-ARGS]
 
sqoop导入时,可以加选择语句,以过滤和综合多表,用–query.也可以只加条件,用–where。这样可以不必每次导入整张表。 如 –where ‘id > 1000′
 示例,采用join选择多表数据:
 sqoop import –query ‘select a.*,b.* from a join b on (a.id == b.id) where $conditions’ -m 1 –target-dir /usr/foo/joinresults

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/8f7170b2780c0e7109619180698fd3d1.html