【HBase】HBase和Sqoop整合


需求一

将mysql表当中的数据导入到HBase中

步骤

一、修改sqoop配置文件

先确定自己sqoop的配置文件已经做了以下配置

cd /export/servers/sqoop-1.4.6-cdh5.14.0/conf
vim sqoop-env.sh
#Set path to where bin/hadoop is available
export HADOOP_COMMON_HOME=/export/servers/hadoop-2.6.0-cdh5.14.0

#Set path to where hadoop-*-core.jar is available
export HADOOP_MAPRED_HOME=/export/servers/hadoop-2.6.0-cdh5.14.0

#set the path to where bin/hbase is available
export HBASE_HOME=/export/servers/hbase-1.2.0-cdh5.14.0

#Set the path to where bin/hive is available
export HIVE_HOME=/export/servers/hive-1.1.0-cdh5.14.0
二、在mysql中创建数据库和数据表并插入数据

别在虚拟机,在自己本机的mysql创建数据库表

CREATE DATABASE  IF NOT EXISTS library;

USE library;

CREATE TABLE book(
id INT(4) PRIMARY KEY NOT NULL AUTO_INCREMENT, 
NAME VARCHAR(255) NOT NULL, 
price VARCHAR(255) NOT NULL);

插入数据

INSERT INTO book(NAME, price) VALUES('Lie Sporting', '30');  
INSERT INTO book (NAME, price) VALUES('Pride & Prejudice', '70');  
INSERT INTO book (NAME, price) VALUES('Fall of Giants', '50'); 
三、将mysql表中的数据导入到HBase表中

在sqoop的目录下执行以下命令 cd /export/servers/sqoop-1.4.6-cdh5.14.0/

bin/sqoop import \
--connect jdbc:mysql://192.168.0.110:3306/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
四、在HBase表中查看数据

在这里插入图片描述


需求二

将HBase当中的数据导出到mysql

步骤

一、创建hive外部表
CREATE EXTERNAL TABLE course.hbase2mysql (
id int,name string,price int)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:name, info:price")
TBLPROPERTIES( "hbase.table.name" = "hbase_book",
"hbase.mapred.output.outputtable" = "hbase2mysql");
二、创建hive内部表并将外部表数据插入到内部表
CREATE TABLE course.hbase2mysqlin(id int,name string,price int);

INSERT OVERWRITE TABLE course.hbase2mysqlin SELECT * FROM course.hbase2mysql;
三、清空mysql表数据
TRUNCATE TABLE book;
四、通过sqoop将hive内部表的数据导出到Mysql
sqoop export -connect jdbc:mysql://192.168.0.110:3306/library -username root -password 123456  -table book -export-dir /user/hive/warehouse/course.db/hbase2mysqlin --input-fields-terminated-by '\001' --input-null-string '\\N' --input-null-non-string '\\N';
五、查询结果

在这里插入图片描述

posted @ 2020-04-03 15:12  _codeRookie  阅读(216)  评论(0编辑  收藏  举报