pandazx's blog

データ分析など雑多な技術ブログ

HBaseでGzip圧縮ありのHFile作成(事前分割)

前回でHBaseのテーブル作成を学んだ。
HBaseでsplit定義ありのテーブル作成 - pandazx's blog

次はデータをインポートするためのHFileを作成する。

以下はポイントだけ記述したMapReduceによるHFile作成方法
(サンプルコード。動作保証なし)

public class CreateHFile extends Configured implements Tool {
	private static final int BLOCK_SIZE = 1 * 1024 * 1024;
	private static final String COMPRESSION_CONF_KEY = "hbase.hfileoutputformat.families.compression";
	private static final String BLOCKSIZE_CONF_KEY = "hbase.mapreduce.hfileoutputformat.blocksize";
	
	@Override
	public int run(String[] args) throws Exception {		
		Configuration conf = getConf();
		Job job = new Job(conf, "CreateHFile");
		job.setJarByClass(CreateHFile.class);

		FileInputFormat.setInputPaths(job, new Path(args[0]));		
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		job.setMapperClass(CreateHFileMapper.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		
		job.setReducerClass(CreateHFileReducer.class);
		job.setOutputKeyClass(ImmutableBytesWritable.class);
		job.setOutputValueClass(KeyValue.class);
		
		// 以下はParthitionerやSortの独自クラスの設定だが、HBaseの場合は以下のページが参考になる
		// http://d.hatena.ne.jp/wyukawa/20121223/1356268112
		job.setPartitionerClass(MyHashPartitioner.class);
		job.setSortComparatorClass(MySortComparator.class);
		job.setGroupingComparatorClass(MyGroupingComparator.class);
		
		// 入出力フォーマット. 作成するCFの圧縮フォーマットはgzip
		job.setInputFormatClass(SequenceFileInputFormat.class);
		job.setOutputFormatClass(HFileOutputFormat.class);
		job.getConfiguration().set(COMPRESSION_CONF_KEY, "CF=gz");
		job.getConfiguration().setInt(BLOCKSIZE_CONF_KEY, BLOCK_SIZE);

		// 出力ファイルをgzip圧縮
		HFileOutputFormat.setCompressOutput(job, true);
		job.getConfiguration().set("hfile.compression", "gz");

		int rtn = job.waitForCompletion(true) ? 0 : -1;
				
		return rtn;
	}

	// CreateHFileMapperクラスやCreateHFileReducerクラスは適宜、作成
}

以下のコマンドでテーブル作成、HFile作成、Bulk Load

export HADOOP_CLASSPATH=${HADOOP_CLASSPATH}:`hbase classpath`
export HBASE_HOME=/usr/lib/hbase
export HBASE_LIB=${HBASE_HOME}/hbase.jar
export HBASE_LOAD_JAR=${HBASE_HOME}/hbase-0.92.1-cdh4.1.2-security.jar
export HBASE_CONF=/etc/hbase/conf/hbase-site.xml

hadoop jar hbase_sample.jar CreateHTable
hadoop jar hbase_sample.jar CreateHFile -libjars ${HBASE_LIB} -conf ${HBASE_CONF} input hfile table_name
hadoop jar ${HBASE_LOAD_JAR} completebulkload -conf ${HBASE_CONF} hfile table_name

MapReduceの設計をテーブル作成時に定義したsplitと同じグルーピングでHFileを作成するようにして、hbase.hregion.max.filesize が十分に大きければ、HBaseへのロードがすぐに終わる。

splitが発生した場合はSplittng...と表示されるので、すぐにわかる。