之前我们已经学习过如何使用Sqoop在Hadoop集群和关系型数据库中进行数据的导入导出工作,接下来我们学习一下利用Sqoop在HBase和RDBMS中进行数据的转储。
说明:只支持关系型数据库往HBase中导数据,反之不支持)
参数 |
描述 |
--column-family |
Sets the target column family for the import 设置导入的目标列族。 |
--hbase-create-table |
If specified, create missing HBase tables 是否自动创建不存在的HBase表(这就意味着,不需要手动提前在HBase中先建立表) |
--hbase-row-key |
Specifies which input column to use as the row key.In case, if input table contains composite key, then comma-separated list of composite key attributes. mysql中哪一列的值作为HBase的rowkey,如果rowkey是个组合键,则以逗号分隔。(注:避免rowkey的重复) |
--hbase-table |
Specifies an HBase table to use as the target instead of HDFS. 指定数据将要导入到HBase中的哪张表中。 |
--hbase-bulkload |
Enables bulk loading. 是否允许bulk形式的导入。 |
1.案例
目标:将RDBMS中的数据抽取到HBase中
(1) 配置sqoop-env.sh,添加如下内容:
export HBASE_HOME=/opt/module/hbase
(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导入数据的操作
$ bin/sqoop import \
--connect jdbc:mysql://hadoop102:3306/db_library \
--username root \
--password 123456 \
--table book \
--columns "id,name,price" \
--column-family "info" \
--hbase-create-table \
--hbase-row-key "id" \
--hbase-table "hbase_book" \
--num-mappers 1 \
--split-by id
提示:sqoop1.4.6只支持HBase1.0.1之前的版本的自动创建HBase表的功能
解决方案:手动创建HBase表
hbase> create 'hbase_book','info'
(5) 在HBase中scan这张表得到如下内容
hbase> scan ‘hbase_book’
最终效果:
hbase(main):005:0> scan 'hbase_book'
ROW COLUMN+CELL
1 column=info:name, timestamp=1537266280769, value=Lie Sporting
1 column=info:price, timestamp=1537266280769, value=30
2 column=info:name, timestamp=1537266280769, value=Pride & Prejudice
2 column=info:price, timestamp=1537266280769, value=70
3 column=info:name, timestamp=1537266280769, value=Fall of Giants
3 column=info:price, timestamp=1537266280769, value=50
3 row(s) in 0.2860 seconds