環境:sqoop1.4.6+hadoop2.6+hbase1.1+mysql5.7
說明:
1.文中的導入導出的表結構借鑑了網上的某篇博客
2.mysql導入hbase能夠直接經過sqoop進行
3.hbase導出到mysql沒法直接進行,須要通過hive的中間做用來完成
hbase→hive外部表→hive內部表→sqoop導出→mysqlmysql
1、Sqoop導入hbase
a) Mysql建立表sql
mysql> create table test.smq_to_hbase select id,name,name grade from test.smq_mysql; mysql> update test.smq_to_hbase set grade = '1'; mysql> Alter table test.smq_to_hbase add primary key(id);
b) Hbase建立表apache
hbase(main):008:0> create 'smq_hbase','info'
c) Sqoop導入hbase中app
[root@master bin]# sqoop import --connect jdbc:mysql://192.168.220.20:3306/test --username root --password 123456 --table smq_to_hbase --hbase-table smq_hbase --column-family info --hbase-row-key id
2、Sqoop導出hbase
Hbase→hive外部表→hive內部表→經過sqoop→mysqloop
a) Mysql建立空表spa
mysql> create table test.employee(rowkey int(11),id int(11),name varchar(20),primary key (id));
b) hbase建立內部表code
hbase(main):001:0> create 'employee','info' hbase(main):002:0> put 'employee',1,'info:id',1 hbase(main):003:0> put 'employee',1,'info:name','peter' hbase(main):004:0> put 'employee',2,'info:id',2 hbase(main):005:0> put 'employee',2,'info:name','paul'
c) hive建立外部表blog
CREATE EXTERNAL TABLE test.h_employee (key int,id int,name string) STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' WITH SERDEPROPERTIES ( "hbase.columns.mapping" = ":key,info:id, info:name" ) TBLPROPERTIES( "hbase.table.name" = "employee", "hbase.mapred.output.outputtable" = "employee");
d) hive建立內部表hadoop
hive> CREATE TABLE test.employee(key INT,id INT,name STRING);
e) hive外部表的數據導入內部表中博客
hive> insert overwrite table test.employee select * from test.h_employee;
f) sqoop導出hive表至mysql中
[root@master bin]# sqoop export -connect jdbc:mysql://192.168.220.20:3306/test -username root -password 123456 -tablemploye