HBase实战 | Hive数据导入云HBase

网络环境

  1. 专线:用户需要把hbase集群的VPC相关网络信息配置到专线里面,可直通hbase环境

  2. 公有云虚拟机VPC环境:选择和hbase通VPC

  3. 其他:需要开hbase公网

  4. 注意:默认导入hbase数据,依赖的hbase-common、hbase-client、hbase-server、hbase-protocol使用社区的包即可。如果是公网需要使用云hbase发布的相关包


方案一:hive关联hbase表方式

  1. 适用场景:数据量不大4T以下(因为需要走hbase的api导入数据)

  2. 从hbase页面获取zk连接地址,并用下述方式启动hive客户端

hive  --hiveconf hbase.zookeeper.quorum=xxxx
  1. hbase表不存在的情况

  • 创建hive表hive_hbase_table映射hbase表base_table,会自动创建hbase表hbase_table,且会随着hive表删除而删除。这里需要指定hive的schema到hbase schema的映射关系。关于类型可参考Hive/HBaseIntegration

CREATE TABLE hive_hbase_table(key int, value string) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val") 
TBLPROPERTIES ("hbase.table.name" = "hbase_table", 
"hbase.mapred.output.outputtable" = "hbase_table");
  • 创建一张原始的hive表,准备一些数据

create table hive_data (mykey int,myval string);insert into hive_data values(1,"www.ymq.io");
  • 把hive原表hive_data中数据,通过hive表hive_hbase_table导入到hbase的表hbase_table中

insert into table hive_hbase_table select * from hive_data;
  • 查看hbase表hbase_table中是否有数据

图片

  1. hbase表存在的情况

  • 创建hive的外表关联hbase表,注意hive schema到hbase schema的映射关系。删除外表不会删除对应hbase表

CREATE EXTERNAL TABLE hive_hbase_external_table(key int, value string) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val") 
TBLPROPERTIES ("hbase.table.name" = "hbase_table", 
"hbase.mapred.output.outputtable" = "hbase_table");
  • 其他导入数据相关同2


方案二:hive表生成hfile,通过bulkload导入到hbase

  1. 适用场景:数据量大(4T以上)

  2. 把hive数据转换为hfile

  • 启动hive并添加相关hbase的jar包

add jar /usr/lib/hive-current/lib/hive-hbase-handler-2.3.3.jar;add jar /usr/lib/hive-current/lib/hbase-common-1.1.1.jar;add jar /usr/lib/hive-current/lib/hbase-client-1.1.1.jar;add jar /usr/lib/hive-current/lib/hbase-protocol-1.1.1.jar;add jar /usr/lib/hive-current/lib/hbase-server-1.1.1.jar;
  • 创建一个outputformat为HiveHFileOutputFormat的hive表

    其中/tmp/hbase_table_hfile/cf_0是hfile保存到hdfs的路径,cf_0是hbase family的名字

create table hbase_hfile_table(key int, cf_0_c0 string) 
stored asINPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'OUTPUTFORMAT 'org.apache.hadoop.hive.hbase.HiveHFileOutputFormat'TBLPROPERTIES ('hfile.family.path' = '/tmp/hbase_table_hfile/cf_0');
  • 把原始数据表的数据通过hbase_hfile_table表保存为hfile

insert into table hbase_hfile_table select * from hive_data;
  • 查看对应hdfs路径是否生成了hfile

图片

  1. 通过bulkload将数据导入到hbase表中

  • 使用阿里云hbase客户端创建具有上面对应family的hbase表

hbase(main):012:0> create 'hbase_hfile_load_table','cf_0'
  • 下载云hbase客户端,配置hbase-site.xml,并将hdfs-site.xml、core-site.xml拷贝到hbase/conf目录

 wget http://public-hbase.oss-cn-hangzhou.aliyuncs.com/installpackage/alihbase-1.1.4-bin.tar.gz .
 vi conf/hbase-site.xml <property>
         <name>hbase.zookeeper.quorum</name>
         <value>xxx</value>
 </property>
  • 执行bulkload导入到hbase表中

bin/hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles hdfs://maste:port/tmp/hbase_table_hfile/  hbase_hfile_load_table
  • 在hbase表hbase_hfile_load_table查看数据是否导入

图片


猜你喜欢

转载自blog.51cto.com/15060465/2677284