sqoop与hbase导入导出数据

环境:sqoop1.4.6+hadoop2.6+hbase1.1+mysql5.7
说明:
1.文中的导入导出的表结构借鉴了网上的某篇博客
2.mysql导入hbase可以直接通过sqoop进行
3.hbase导出到mysql无法直接进行,需要经过hive的中间作用来完成
hbase→hive外部表→hive内部表→sqoop导出→mysql

一、Sqoop导入hbase
a) Mysql创建表

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创建表

hbase(main):008:0> create 'smq_hbase','info'

c) Sqoop导入hbase中

[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

二、Sqoop导出hbase
Hbase→hive外部表→hive内部表→通过sqoop→mysql

a) Mysql创建空表

mysql> create table test.employee(rowkey int(11),id int(11),name varchar(20),primary key (id));

b) hbase创建内部表

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创建外部表

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创建内部表

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
原文地址:https://www.cnblogs.com/felixzh/p/11362183.html