2.7、与Sqoop的集成
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中先建立表) |
--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形式的导入。 |
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) 在HBase中scan这张表得到如下内容
hbase> scan ‘hbase_book’ |
思考:尝试使用复合键作为导入数据时的rowkey。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2018-12-20 cqrs案例
2018-12-20 Callable和Future 多线程