sqoop 集成 hbase(1)

2.7、与Sqoop的集成

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

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

相关参数:

参数

描述

--column-family <family>

Sets the target column family for the import

设置导入的目标列族。

--hbase-create-table

If specified, create missing HBase tables

是否自动创建不存在的HBase表(这就意味着,不需要手动提前在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中哪一列的值作为HBaserowkey,如果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形式的导入。

1) 案例

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

分步实现:

(1) 配置sqoop-env.sh,添加如下内容:

export HBASE_HOME=/home/admin/modules/hbase-1.3.6

(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://hadoop001:3306/db_library

--username root

--password root

--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) HBasescan这张表得到如下内容

hbase> scan ‘hbase_book’

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

原文地址:https://www.cnblogs.com/lshan/p/12072320.html