关系型数据库Mysql和Oracle与HBase导入导出数据

  1. MySQL与HBase导入导出数据

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

 

  1. MySQL导出至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

 

  1. 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';

 

  1. 将ORACLE数据导入HBase
  2. 简介及部分重要参数介绍

        Sqoop除了能够将数据从关系型数据库导入到HDFS和Hive中,还能够导入到HBase表中。

       --hbase-table:通过指定--hbase-table参数值,指明将数据导入到HBase表中,而不是HDFS上的一个目录。输入表中的每一行将会被转换成一个HBase Put操作的输出表的一行。

       --hbase-row-key:你可以使用--hbase-row-key参数,手动的指定row key。默认的情况下,Sqoop会将split-by 列作为HBase rowkey列。如果没有指定split-by值,它将会试图识别关系表的关键字。

       如果源表是组合关键字,--hbase-row-key 参数后面值是用逗号分隔的组合关键字属性的列表,在这样种情况下,通过合并组合关键字属性的值来产生HBase的Row key,每个值之间使用下划线分隔开来。

       --column-family:必须指定--column-family参数,每一个输出列都会被放到同一个family列族中。 

      --hbase-create-table:如果HBase中的目标表和列族不存在,如果你使用该参数,Sqoop在运行任务的时候会根据HBase的默认配置,首先创建目标表和列族。

       注意一:当源表中是组合关键字的时候,必须手动指定--hbase-row-key参数,Sqoop才能将数据导入到HBase中,否则不行。

       注意二:如果HBase中的目标表和列族不存在,如果没加--hbase-create-table参数,Sqoop job将会报错误退出运行。所以你在将数据从源表导入到HBase之前,需要首先在HBase中创建目标表和其对应的列族。

      Sqoop目前会序列化所有的字段值,将值转换为字符串表示,然后向HBase中插入UTF-8编码的字符串值的二进制值。

 

  1. 原始数据如下

 

其中id是主键

  1. 以单关键字作为Rowkey导入

sqoop import --append --connect jdbc:oracle:thin:@219.216.110.120:1521:orcl --username TEST1 --password test1 --table TEST1 --columns age  --hbase-table test1 --hbase-row-key id --column-family personinfo

hbase(main):005:0> scan 'test1'

ROW  COLUMN+CELL                                              

 1   column=personinfo:name, timestamp=1462411628085, value=\xE

                      5\xBC\xA0\xE4\xB8\x89                                    

 1   column=personinfo:sex, timestamp=1462411628085, value=\xE7

                      \x94\xB7                                                 

 2   column=personinfo:name, timestamp=1462411628085, value=\xE

                      6\x9D\x8E\xE5\x9B\x9B                                     

 2   column=personinfo:sex, timestamp=1462411628085, value=\xE5

                      \xA5\xB3                                                 

 3   column=personinfo:name, timestamp=1462411625239, value=\xE

                      7\x8E\x8B\xE4\xBA\x94                                    

 4   column=personinfo:name, timestamp=1462411623526, value=\xE

                      8\xB5\xB5\xE5\x85\xAD                                    

 4   column=personinfo:sex, timestamp=1462411623526, value=\xE7

                      \x94\xB7                                                 

 5     column=personinfo:name, timestamp=1462411628403, value=\xE

                      7\x94\xB0\xE4\xB8\x83                                     

 5     column=personinfo:sex, timestamp=1462411628403, value=\xE5

                      \xA5\xB3                                                 

 6     column=personinfo:name, timestamp=1462411628403, value=\xE

                      7\x8E\x8B\xE5\x85\xAB                                    

6 row(s) in 0.4110 seconds

  1. 手动指定Rowkey

建立test2表,演示手动指定Rowkey的情形。我们以id+name组合表示RowKey,如下:

sqoop import --append --connect jdbc:oracle:thin:@219.216.110.120:1521:orcl --username TEST1 --password test1 --table TEST1 --columns id,name,sex,age --hbase-create-table --hbase-table test2 --hbase-row-key id,name --column-family personinfo

查看结果:

hbase(main):006:0> scan 'test2'

ROW   COLUMN+CELL                                              

 1_\xE5\xBC\xA0\xE4\x column=personinfo:age, timestamp=1462413115207, value=20B8\x89

 1_\xE5\xBC\xA0\xE4\x column=personinfo:sex, timestamp=1462413115207, value=\xE7 B8\x89\x94\xB7

 2_\xE6\x9D\x8E\xE5\x column=personinfo:age, timestamp=1462413115207, value=259B\x9B                                                                        

 2_\xE6\x9D\x8E\xE5\x column=personinfo:sex, timestamp=1462413115207, value=\xE59B\x9B\xA5\xB3                                                 

 3_\xE7\x8E\x8B\xE4\x column=personinfo:age, timestamp=1462413102229, value=65 BA\x94                                                                        

 4_\xE8\xB5\xB5\xE5\x column=personinfo:sex, timestamp=1462413115746, value=\xE7 85\xAD\x94\xB7                                                 

 5_\xE7\x94\xB0\xE4\x column=personinfo:age, timestamp=1462413120206, value=45B8\x83                                                                     

 5_\xE7\x94\xB0\xE4\x column=personinfo:sex, timestamp=1462413120206, value=\xE5 B8\x83\xA5\xB3                                                 

5 row(s) in 0.1520 seconds

由上可知,数据也成功导入到了HBase中,且Rowkey为id号+name列值的二进制值的组合。Rowkey列之间值是以下划线分隔的。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值