原帖地址: http://blog.csdn.net/nsrainbow/article/details/41649671
承接上节课,继续讲如何使用sqoop将mysql的数据导入到 Hbase 或者 Hive 里面
CREATE TABLE `employee` ( `id` int(11) NOT NULL, `name` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
insert into employee (id,name) values (1,'michael'); insert into employee (id,name) values (2,'ted'); insert into employee (id,name) values (3,'jack');
hbase(main):006:0> create 'employee','info' 0 row(s) in 0.4440 seconds => Hbase::Table - employee
# sqoop import --connect jdbc:mysql://host1:3306/sqoop_test --username root --password root --table employee --hbase-table employee --column-family info --hbase-row-key id -m 1
………… ); retry policy is RetryUpToMaximumCountWithFixedSleep(maxRetries=3, sleepTime=1000 MILLISECONDS) 15/01/23 07:56:49 INFO mapred.ClientServiceDelegate: Application state is completed. FinalApplicationStatus=SUCCEEDED. Redirecting to job history server 15/01/23 07:57:16 INFO mapreduce.ImportJobBase: Transferred 0 bytes in 1,315.279 seconds (0 bytes/sec) 15/01/23 07:57:16 INFO mapreduce.ImportJobBase: Retrieved 3 records.
hbase(main):001:0> scan 'employee' SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/lib/hadoop/lib/slf4j-log4j12.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/lib/zookeeper/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] ROW COLUMN+CELL 1 column=info:name, timestamp=1417426628685, value=michael 2 column=info:name, timestamp=1417426628685, value=ted 3 column=info:name, timestamp=1417426628685, value=jack 3 row(s) in 0.1630 seconds
# sqoop import --connect jdbc:mysql://host1:3306/sqoop_test --username root --password root --table employee --hive-import --hive-table hive_employee --create-hive-table
hive> select * from hive_employee; OK 1 michael 2 ted 3 jack Time taken: 0.179 seconds, Fetched: 3 row(s)
CREATE TABLE `people` ( `id` int(11) NOT NULL, `name` varchar(20) NOT NULL, `year` varchar(10), PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; insert into people values (1,'jack','2015','01','02'); insert into people values (2,'ted','2015','01','02'); insert into people values (3,'billy','2015','01','02'); insert into people values (4,'sara','2015','01','03');
sqoop import --connect jdbc:mysql://xmseapp03:3306/sqoop_test --username sqoop --password sqoop --query 'select id,name from people where year="2015" AND $CONDITIONS' --direct -m 2 --split-by id --hive-import --create-hive-table --hive-table hive_people --target-dir /user/hive_people --hive-partition-key year --hive-partition-value '2015'
hive> select * from hive_people; OK 1 jack 2015 2 ted 2015 3 billy 2015 4 sara 2015 Time taken: 1.041 seconds, Fetched: 4 row(s)
[root@host1 ~]$ hdfs dfs -cat /user/hive/warehouse/hive_people/year=2015/part-m-00000 1jack 2ted [root@host1 ~]$ hdfs dfs -cat /user/hive/warehouse/hive_people/year=2015/part-m-00001 3billy 4sara