6 HBase的Bulkload
HBase可以让我们随机的、实时的访问大数据,但是怎样有效的将数据导入到HBase呢?HBase有多种导入数据的方法,最直接的方法就是在MapReduce作业中使用TableOutputFormat作为输出,或者使用标准的客户端API,但是这些都不是非常有效的方法。
如果HDFS中有海量数据要导入HBase,可以先将这些数据生成HFile文件,然后批量导入HBase的数据表中,这样可以极大地提升数据导入HBase的效率。这就是HBase的Bulkload,即利用MapReduce作业输出HBase内部数据格式的表数据,然后将生成的StoreFiles直接导入到集群中。与使用HBase API相比,使用Bulkload导入数据占用更少的CPU和网络资源。两个表之间的数据迁移也可以使用这种方法。下面给出具体示例:
package com.hbase.demo; import java.io.IOException; import org.apache.Hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.io.ImmutableBytesWritable; import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat2; import org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles; import org.apache.hadoop.hbase.mapreduce.PutSortReducer; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class HBaseBulk { public static class bulkMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, Put> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // 将输入数据用tab键分词 String[] values = value.toString().split("\t"); if (values.length == 2) { // 设置行键、列族、列名和值 byte[] rowKey = Bytes.toBytes(values[0]); byte[] family = Bytes.toBytes("content"); byte[] column = Bytes.toBytes("number"); byte[] colValue = Bytes.toBytes(values[1]); // 将行键序列化作为mapper输出的key ImmutableBytesWritable rowKeyWritable = new ImmutableBytesWritable(rowKey); // 将put对象作为mapper输出的value Put put = new Put(rowKey); put.addColumn(family, column, colValue); context.write(rowKeyWritable, put); } } } @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { if (args.length != 3) { System.out.println("args error"); System.exit(0); } String input = args[0]; String output = args[1]; String jobName = args[2]; String tableName = args[3]; // 配置MapReduce(或者将hadoop的相关配置文件引入项目) Configuration hadoopConf = new Configuration(); hadoopConf.set("fs.defaultFS", "localhost:9000"); hadoopConf.set("mapred.job.tracker", "localhost:9001"); Job job = Job.getInstance(hadoopConf, jobName); job.setJarByClass(HBaseBulk.class); job.setMapperClass(bulkMapper.class); job.setReducerClass(PutSortReducer.class); job.setMapOutputKeyClass(ImmutableBytesWritable.class); job.setMapOutputValueClass(Put.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(HFileOutputFormat2.class); FileInputFormat.addInputPath(job, new Path(input)); FileOutputFormat.setOutputPath(job, new Path(output)); // 配置HBase(或者将hbase的相关配置文件引入项目) Configuration hbaseConf = HBaseConfiguration.create(); hbaseConf.set("hbase.zookeeper.quorum", "localhost"); hbaseConf.set("hbase.zookeeper.property.clientPort", "2181"); // 生成HFile Connection conn = ConnectionFactory.createConnection(hbaseConf); HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); HFileOutputFormat2.configureIncrementalLoad(job, table); // 执行任务 job.waitForCompletion(true); // 将HFile文件导入HBase LoadIncrementalHFiles loader = new LoadIncrementalHFiles(hbaseConf); loader.doBulkLoad(new Path(output), table); } }
上述代码首先将HDFS中的数据文件通过MapReduce任务生成HFile文件,然后将HFile文件导入HBase数据表(该数据表已存在)。HDFS中的数据文件和导入HBase后的数据表分别如下图所示:


HBase 的详细介绍:请点这里
HBase 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-08/134181.htm
