Hbase-Sqoop集成Hbase

Posted on 2020-04-29 16:56  MissRong  阅读(126)  评论(0)    收藏  举报

Sqoop集成:MySQL TO HBase

Sqoop supports additional import targets beyond HDFS and Hive. Sqoop can also import records into a table in HBase.

之前我们已经学习过如何使用Sqoop在Hadoop集群和关系型数据库中进行数据的导入导出工作,接下来我们学习一下利用Sqoop在HBase和RDBMS中进行数据的转储。

相关参数:

参数

描述

--column-family <family>

Sets the target column family for the import

设置导入的目标列族。

--hbase-create-table

If specified, create missing HBase tables

是否自动创建不存在的HBase表(这就意味着,不需要手动提前在HBase中先建立表)

只适用于0.9以前的Hbase

--hbase-row-key <col>

Specifies which input column to use as the row key.In case, if input table contains composite

key, then <col> must be in the form of a

comma-separated list of composite key

attributes.

mysql中哪一列的值作为HBase的rowkey,如果rowkey是个组合键,则以逗号分隔。(注:避免rowkey的重复)

--hbase-table <table-name>

Specifies an HBase table to use as the target instead of HDFS.

指定数据将要导入到HBase中的哪张表中。

--hbase-bulkload

Enables bulk loading.

是否允许bulk形式的导入。

案例

目标:将RDBMS中的数据抽取到HBase中

分步实现:

(1) 配置sqoop-env.sh

[root@bigdata111 sqoop-1.4.6]# vi conf/sqoop-env.sh

添加如下内容:

export HBASE_HOME=/opt/module/hbase-1.3.1

                       

 

 (2) Mysql中新建一个数据库db_library,一张表book

CREATE DATABASE db_library;

CREATE TABLE db_library.book(

id int(4) PRIMARY KEY NOT NULL AUTO_INCREMENT,

name VARCHAR(255) NOT NULL,

price VARCHAR(255) NOT NULL);

(3) 向表中插入一些数据

INSERT INTO db_library.book (name, price) VALUES('Lie Sporting', '30');  

INSERT INTO db_library.book (name, price) VALUES('Pride & Prejudice', '70');  

INSERT INTO db_library.book (name, price) VALUES('Fall of Giants', '50');

(4) 执行Sqoop导入数据的操作

手动创建HBase表

hbase> create 'hbase_book','info'

(5) HBasescan这张表得到如下内容

hbase> scan 'hbase_book'

思考:尝试使用复合键作为导入数据时的rowkey。

$ bin/sqoop import \

--connect jdbc:mysql://bigdata111:3306/db_library \

--username root \

--password 000000 \

--table book \

--columns "id,name,price" \  #关系型数据库中的字段

--column-family "info" \  #列簇

--hbase-create-table \  #老的Hbase可以直接创建

--hbase-row-key "id" \

--hbase-table "hbase_book" \

--num-mappers 1 \

--split-by id

尖叫提示:sqoop1.4.6只支持HBase1.0.1之前的版本的自动创建HBase表的功能

***************************************************************************

自己实现:

(1)这里可以直接用之前mysql数据库中的表-student:

[root@bigdata111 sqoop-1.4.6]# mysql -uroot -p

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| azkaban            |

| metastore          |

| mysql              |

| performance_schema |

| sys                |

| test               |

+--------------------+

7 rows in set (0.52 sec)

 

退出:quit

(2)尝试使用复合键作为导入数据时的rowKey

注意:不能从Word里直接复制,要使用Notepad++进行复制粘贴。

这里就是将MySQL关系数据库中test数据库的student表的数据导入到hbase中(hbase中要创建的表是根据MySQl的student表创建的)

sqoop import \

--connect jdbc:mysql://bigdata111:3306/test \

--username root \

--password 000000 \

--table student \

--columns "id,name,sex" \

--column-family "info" \

--hbase-create-table \

--hbase-row-key "id" \

--hbase-table "hbase_student" \

--num-mappers 1 \

--split-by id

(3)报错:一个关于main的错误提示

在hbase中创建表hbase-student:

hbase(main):009:0> create 'hbase_student','info'

(4)再次执行、查看结果:

 

发现Hbase中,显示不出中文。

(5)下面换一张全英文的表进行操作:

 

(6)再新建一张表、执行查看:

hbase(main):017:0> create 'hbase_student2','info'

执行命令-利用SqoopMySQL数据库中test数据库的student2表中的数据加载到Hbase的hbase_student2表中:

sqoop import \

--connect jdbc:mysql://bigdata111:3306/test \

--username root \

--password 000000 \

--table student2 \

--columns "id,name" \

--column-family "info" \

--hbase-create-table \

--hbase-row-key "id" \

--hbase-table "hbase_student2" \

--num-mappers 1 \

--split-by id

查看结果:

 

 

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3