MapReduce生成HFile入庫到HBase


個人小站,正在持續整理中,歡迎訪問:http://shitouer.cn

小站博文地址:MapReduce生成HFile入庫到HBase

一、這種方式有很多的優點:

1. 如果我們一次性入庫hbase巨量數據,處理速度慢不說,還特別占用Region資源, 一個比較高效便捷的方法就是使用 “Bulk Loading”方法,即HBase提供的HFileOutputFormat類。

2. 它是利用hbase的數據信息按照特定格式存儲在hdfs內這一原理,直接生成這種hdfs內存儲的數據格式文件,然后上傳至合適位置,即完成巨量數據快速入庫的辦法。配合mapreduce完成,高效便捷,而且不占用region資源,增添負載。

二、這種方式也有很大的限制:

1. 僅適合初次數據導入,即表內數據為空,或者每次入庫表內都無數據的情況。

2. HBase集群與Hadoop集群為同一集群,即HBase所基於的HDFS為生成HFile的MR的集群(額,咋表述~~~)

三、接下來一個demo,簡單介紹整個過程。

1. 生成HFile部分

package zl.hbase.mr;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat;
import org.apache.hadoop.hbase.mapreduce.KeyValueSortReducer;
import org.apache.hadoop.hbase.mapreduce.SimpleTotalOrderPartitioner;
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import zl.hbase.util.ConnectionUtil;

public class HFileGenerator {

	public static class HFileMapper extends
			Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue> {
		@Override
		protected void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			String line = value.toString();
			String[] items = line.split(",", -1);
			ImmutableBytesWritable rowkey = new ImmutableBytesWritable(
					items[0].getBytes());

			KeyValue kv = new KeyValue(Bytes.toBytes(items[0]),
					Bytes.toBytes(items[1]), Bytes.toBytes(items[2]),
					System.currentTimeMillis(), Bytes.toBytes(items[3]));
			if (null != kv) {
				context.write(rowkey, kv);
			}
		}
	}

	public static void main(String[] args) throws IOException,
			InterruptedException, ClassNotFoundException {
		Configuration conf = new Configuration();
		String[] dfsArgs = new GenericOptionsParser(conf, args)
				.getRemainingArgs();

		Job job = new Job(conf, "HFile bulk load test");
		job.setJarByClass(HFileGenerator.class);

		job.setMapperClass(HFileMapper.class);
		job.setReducerClass(KeyValueSortReducer.class);

		job.setMapOutputKeyClass(ImmutableBytesWritable.class);
		job.setMapOutputValueClass(Text.class);

		job.setPartitionerClass(SimpleTotalOrderPartitioner.class);

		FileInputFormat.addInputPath(job, new Path(dfsArgs[0]));
		FileOutputFormat.setOutputPath(job, new Path(dfsArgs[1]));

		HFileOutputFormat.configureIncrementalLoad(job,
				ConnectionUtil.getTable());
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}

生成HFile程序說明:

①. 最終輸出結果,無論是map還是reduce,輸出部分key和value的類型必須是: < ImmutableBytesWritable, KeyValue>或者< ImmutableBytesWritable, Put>。

②. 最終輸出部分,Value類型是KeyValue 或Put,對應的Sorter分別是KeyValueSortReducer或PutSortReducer。

③. MR例子中job.setOutputFormatClass(HFileOutputFormat.class); HFileOutputFormat只適合一次對單列族組織成HFile文件。

④. MR例子中HFileOutputFormat.configureIncrementalLoad(job, table);自動對job進行配置。SimpleTotalOrderPartitioner是需要先對key進行整體排序,然后划分到每個reduce中,保證每一個reducer中的的key最小最大值區間范圍,是不會有交集的。因為入庫到HBase的時候,作為一個整體的Region,key是絕對有序的。

⑤. MR例子中最后生成HFile存儲在HDFS上,輸出路徑下的子目錄是各個列族。如果對HFile進行入庫HBase,相當於move HFile到HBase的Region中,HFile子目錄的列族內容沒有了。

2. HFile入庫到HBase

package zl.hbase.bulkload;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles;
import org.apache.hadoop.util.GenericOptionsParser;

import zl.hbase.util.ConnectionUtil;

public class HFileLoader {

	public static void main(String[] args) throws Exception {
		String[] dfsArgs = new GenericOptionsParser(
				ConnectionUtil.getConfiguration(), args).getRemainingArgs();
		LoadIncrementalHFiles loader = new LoadIncrementalHFiles(
				ConnectionUtil.getConfiguration());
		loader.doBulkLoad(new Path(dfsArgs[0]), ConnectionUtil.getTable());
	}

}

通過HBase中 LoadIncrementalHFiles的doBulkLoad方法,對生成的HFile文件入庫

 

原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://shitouer.cn/2013/02/hbase-hfile-bulk-load/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM