HBase BulkLoad批量写入数据实战

1.概述

在进行数据传输中,批量加载数据到HBase集群有多种方式,好比经过HBase API进行批量写入数据、使用Sqoop工具批量导数到HBase集群、使用MapReduce批量导入等。这些方式,在导入数据的过程当中,若是数据量过大,可能耗时会比较严重或者占用HBase集群资源较多(如磁盘IO、HBase Handler数等)。今天这篇博客笔者将为你们分享使用HBase BulkLoad的方式来进行海量数据批量写入到HBase集群。apache

2.内容

在使用BulkLoad以前,咱们先来了解一下HBase的存储机制。HBase存储数据其底层使用的是HDFS来做为存储介质,HBase的每一张表对应的HDFS目录上的一个文件夹,文件夹名以HBase表进行命名(若是没有使用命名空间,则默认在default目录下),在表文件夹下存放在若干个Region命名的文件夹,Region文件夹中的每一个列簇也是用文件夹进行存储的,每一个列簇中存储就是实际的数据,以HFile的形式存在。路径格式以下:app

/hbase/data/default/<tbl_name>/<region_id>/<cf>/<hfile_id>

2.1 实现原理

按照HBase存储数据按照HFile格式存储在HDFS的原理,使用MapReduce直接生成HFile格式的数据文件,而后在经过RegionServer将HFile数据文件移动到相应的Region上去。流程以下图所示:dom

2.2. 生成HFile文件

HFile文件的生成,可使用MapReduce来进行实现,将数据源准备好,上传到HDFS进行存储,而后在程序中读取HDFS上的数据源,进行自定义封装,组装RowKey,而后将封装后的数据在回写到HDFS上,以HFile的形式存储到HDFS指定的目录中。实现代码以下:tcp

/**
 * Read DataSource from hdfs & Gemerator hfile.
 * 
 * @author smartloli.
 *
 *         Created by Aug 19, 2018
 */
public class GemeratorHFile2 {
    static class HFileImportMapper2 extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> {
        
        protected final String CF_KQ = "cf";

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            System.out.println("line : " + line);
            String[] datas = line.split(" ");
            String row = new Date().getTime() + "_" + datas[1];
            ImmutableBytesWritable rowkey = new ImmutableBytesWritable(Bytes.toBytes(row));
            KeyValue kv = new KeyValue(Bytes.toBytes(row), this.CF_KQ.getBytes(), datas[1].getBytes(), datas[2].getBytes());
            context.write(rowkey, kv);
        }
    }

    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("<Usage>Please input hbase-site.xml path.</Usage>");
            return;
        }
        Configuration conf = new Configuration();
        conf.addResource(new Path(args[0]));
        conf.set("hbase.fs.tmp.dir", "partitions_" + UUID.randomUUID());
        String tableName = "person";
        String input = "hdfs://nna:9000/tmp/person.txt";
        String output = "hdfs://nna:9000/tmp/pres";
        System.out.println("table : " + tableName);
        HTable table;
        try {
            try {
                FileSystem fs = FileSystem.get(URI.create(output), conf);
                fs.delete(new Path(output), true);
                fs.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            Connection conn = ConnectionFactory.createConnection(conf);
            table = (HTable) conn.getTable(TableName.valueOf(tableName));
            Job job = Job.getInstance(conf);
            job.setJobName("Generate HFile");

            job.setJarByClass(GemeratorHFile2.class);
            job.setInputFormatClass(TextInputFormat.class);
            job.setMapperClass(HFileImportMapper2.class);
            FileInputFormat.setInputPaths(job, input);
            FileOutputFormat.setOutputPath(job, new Path(output));

            HFileOutputFormat2.configureIncrementalLoad(job, table);
            try {
                job.waitForCompletion(true);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

在HDFS目录/tmp/person.txt中,准备数据源以下:ide

1 smartloli 100
2 smartloli2 101
3 smartloli3 102

而后,将上述代码编译打包成jar,上传到Hadoop集群进行执行,执行命令以下:工具

hadoop jar GemeratorHFile2.jar /data/soft/new/apps/hbaseapp/hbase-site.xml

若是在执行命令的过程当中,出现找不到类的异常信息,多是本地没有加载HBase依赖JAR包,在当前用户中配置以下环境变量信息:oop

export HADOOP_CLASSPATH=$HBASE_HOME/lib/*:classpath

而后,执行source命令使配置的内容当即生生效。学习

2.3. 执行预览

在成功提交任务后,Linux控制台会打印执行任务进度,也能够到YARN的资源监控界面查看执行进度,结果以下所示:大数据

等待任务的执行,执行完成后,在对应HDFS路径上会生成相应的HFile数据文件,以下图所示:this

2.4 使用BulkLoad导入到HBase

而后,在使用BulkLoad的方式将生成的HFile文件导入到HBase集群中,这里有2种方式。一种是写代码实现导入,另外一种是使用HBase命令进行导入。

2.4.1 代码实现导入

经过LoadIncrementalHFiles类来实现导入,具体代码以下:

/**
* Use BulkLoad inport hfile from hdfs to hbase.
* 
* @author smartloli.
*
* Created by Aug 19, 2018
*/
public class BulkLoad2HBase {

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.out.println("<Usage>Please input hbase-site.xml path.</Usage>");
            return;
        }
        String output = "hdfs://cluster1/tmp/pres";
        Configuration conf = new Configuration();
        conf.addResource(new Path(args[0]));
        HTable table = new HTable(conf, "person");
        LoadIncrementalHFiles loader = new LoadIncrementalHFiles(conf);
        loader.doBulkLoad(new Path(output), table);
    }
    
}

执行上述代码,运行结果以下:

2.4.2 使用HBase命令进行导入

先将生成好的HFile文件迁移到目标集群(即HBase集群所在的HDFS上),而后在使用HBase命令进行导入,执行命令以下:

# 先使用distcp迁移hfile
hadoop distcp -Dmapreduce.job.queuename=queue_1024_01 -update -skipcrccheck -m 10 /tmp/pres hdfs://nns:9000/tmp/pres

# 使用bulkload方式导入数据
hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles /tmp/pres person

最后,咱们能够到指定的RegionServer节点上查看导入的日志信息,以下所示为导入成功的日志信息:

2018-08-19 16:30:34,969 INFO  [B.defaultRpcServer.handler=7,queue=1,port=16020] regionserver.HStore: Successfully loaded store file hdfs://cluster1/tmp/pres/cf/7b455535f660444695589edf509935e9 into store cf (new location: hdfs://cluster1/hbase/data/default/person/2d7483d4abd6d20acdf16533a3fdf18f/cf/d72c8846327d42e2a00780ac2facf95b_SeqId_4_)

2.5 验证

使用BulkLoad方式导入数据后,能够进入到HBase集群,使用HBase Shell来查看数据是否导入成功,预览结果以下:

3.总结

本篇博客为了演示实战效果,将生成HFile文件和使用BulkLoad方式导入HFile到HBase集群的步骤进行了分解,实际状况中,能够将这两个步骤合并为一个,实现自动化生成与HFile自动导入。若是在执行的过程当中出现RpcRetryingCaller的异常,能够到对应RegionServer节点查看日志信息,这里面记录了出现这种异常的详细缘由。

4.结束语

这篇博客就和你们分享到这里,若是你们在研究学习的过程中有什么问题,能够加群进行讨论或发送邮件给我,我会尽我所能为您解答,与君共勉!

另外,博主出书了《Hadoop大数据挖掘从入门到进阶实战》,喜欢的朋友或同窗, 能够在公告栏那里点击购买连接购买博主的书进行学习,在此感谢你们的支持。

相关文章
相关标签/搜索