Hive数据导入Hbase

方案一:Hive关联HBase表方式apache

适用场景:数据量不大4T如下(走hbase的api导入数据)api

1、hbase表不存在的状况app

建立hive表hive_hbase_table映射hbase表hbase_table,会自动建立hbase表hbase_table,且会随着hive表删除而删除,这里须要指定hive的schema到hbase schema的映射关系:oop

一、建表spa

CREATE TABLE hive_hbase_table(key int, name String,age String) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:name,cf1:age") 
TBLPROPERTIES ("hbase.table.name" = "hbase_table", 
"hbase.mapred.output.outputtable" = "hbase_table");

二、建立一张原始的hive表,准备一些数据code

create table hive_data (key int,name String,age string);
insert into hive_data values(1,"za","13");
insert into hive_data values(2,"ff","44");

三、把hive原表hive_data的数据,经过hive表hive_hbase_table导入到hbase的表hbase_table中orm

insert into table hive_hbase_table select * from hive_data;

四、查看hbase表hbase_table中是否有数据server

2、hbase表存在的状况xml

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

CREATE EXTERNAL TABLE hive_hbase_external_table(key String, name string,sex String,age String,department String) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,info:name,info:sex,info:age,info:department") 
TBLPROPERTIES ("hbase.table.name" = "filtertest", 
"hbase.mapred.output.outputtable" = "filtertest");

其余步骤与上面相同

方案二:HIve表生成hfile,经过bulkload导入到hbase

一、适用场景:数据量大(4T以上)

二、把hive数据转换为hfile

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

add jar /mnt/hive/lib/hive-hbase-handler-2.1.1.jar;
add jar /mnt/hive/lib/hbase-common-1.1.1.jar;
add jar /mnt/hive/lib/hbase-client-1.1.1.jar;
add jar /mnt/hive/lib/hbase-protocol-1.1.1.jar;
add jar /mnt/hive/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, name string,age String) 
stored as
INPUTFORMAT '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

 

七、经过bulkload将数据导入到hbase表中

建表:使用hbase客户端建立具备上面对应family的hbase表

create 'hbase_hfile_load_table','cf_0'

下载hbase客户端,配置hbase-site.xml,并将hdfs-site.xml、core-site.xml拷贝到hbase/conf目录

导入:

 hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles \
hdfs://master:9000/tmp/hbase_table_hfile/ hbase_hfile_load_table

 八、查看

相关文章
相关标签/搜索