Advertisement

sqoop与hbase导入导出数据

阅读量:

环境:sqoop1.4.6+hadoop2.6+hbase1.1+mysql5.7
说明:
该表结构参考自某知名博客文章中提到的一种常见数据交互模式。
该系统架构支持通过Sqoop工具实现MySQL向HBase的数据导入过程。
需要注意的是,在使用HBase与MySQL交互时,默认情况下其数据传输功能存在一定的限制:例如,在HBase向MySQL迁移数据时,默认情况下无法直接完成这一操作;此时可采取中间层解决方案(如引入Hive表)来进行数据迁移操作:具体流程包括以下步骤:

  1. HBase创建外部表并将其映射到hive表
  2. 将hive表转换为内部表
  3. 使用Sqoop工具将hive内部表导出为MySQL可用格式
  4. 最终完成HBase到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  -tablemployee -export-dir /user/hive/warehouse/test.db/employee --input-fields-terminated-by '\001' --input-null-string '\ N' --input-null-non-string '\ N';

全部评论 (0)

还没有任何评论哟~