From bb0018b97283e5a6b4372b08f7379f2376862912 Mon Sep 17 00:00:00 2001 From: chenyechao Date: Mon, 11 Mar 2019 21:37:23 +0800 Subject: [PATCH] HBASE-21810 bulkload support set hfile compression on client --- .../hadoop/hbase/mapreduce/HFileOutputFormat2.java | 14 ++++++- .../hbase/mapreduce/TestHFileOutputFormat2.java | 43 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java index d315a6d..174c988 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/mapreduce/HFileOutputFormat2.java @@ -105,9 +105,11 @@ public class HFileOutputFormat2 // This constant is public since the client can modify this when setting // up their conf object and thus refer to this symbol. // It is present for backwards compatibility reasons. Use it only to - // override the auto-detection of datablock encoding. + // override the auto-detection of datablock encoding and compression. public static final String DATABLOCK_ENCODING_OVERRIDE_CONF_KEY = "hbase.mapreduce.hfileoutputformat.datablock.encoding"; + public static final String COMPRESSION_OVERRIDE_CONF_KEY = + "hbase.mapreduce.hfileoutputformat.compression"; @Override public RecordWriter getRecordWriter( @@ -131,6 +133,13 @@ public class HFileOutputFormat2 Compression.Algorithm.NONE.getName()); final Algorithm defaultCompression = AbstractHFileWriter .compressionByName(defaultCompressionStr); + String compressionStr = conf.get(COMPRESSION_OVERRIDE_CONF_KEY); + final Algorithm overriddenCompression; + if (compressionStr != null) { + overriddenCompression = Compression.getCompressionAlgorithmByName(compressionStr); + } else { + overriddenCompression = null; + } final boolean compactionExclude = conf.getBoolean( "hbase.mapreduce.hfileoutputformat.compaction.exclude", false); @@ -227,7 +236,8 @@ public class HFileOutputFormat2 throws IOException { WriterLength wl = new WriterLength(); Path familydir = new Path(outputdir, Bytes.toString(family)); - Algorithm compression = compressionMap.get(family); + Algorithm compression = overriddenCompression; + compression = compression == null ? compressionMap.get(family) : compression; compression = compression == null ? defaultCompression : compression; BloomType bloomType = bloomTypeMap.get(family); bloomType = bloomType == null ? BloomType.NONE : bloomType; diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestHFileOutputFormat2.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestHFileOutputFormat2.java index 95c753a..1548e59 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestHFileOutputFormat2.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/mapreduce/TestHFileOutputFormat2.java @@ -40,7 +40,9 @@ import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.LocatedFileStatus; import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.RemoteIterator; import org.apache.hadoop.hbase.CategoryBasedTimeout; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; @@ -1109,5 +1111,46 @@ public class TestHFileOutputFormat2 { } } + @Test + public void TestConfigureCompression() throws Exception { + Configuration conf = new Configuration(this.util.getConfiguration()); + RecordWriter writer = null; + TaskAttemptContext context = null; + Path dir = util.getDataTestDir("TestConfigureCompression"); + String hfileoutputformatCompression = "gz"; + + try { + + conf.set(HFileOutputFormat2.COMPRESSION_OVERRIDE_CONF_KEY, hfileoutputformatCompression); + + Job job = Job.getInstance(conf); + FileOutputFormat.setOutputPath(job, dir); + context = createTestTaskAttemptContext(job); + HFileOutputFormat2 hof = new HFileOutputFormat2(); + writer = hof.getRecordWriter(context); + final byte[] b = Bytes.toBytes("b"); + + KeyValue kv = new KeyValue(b, b, b, HConstants.LATEST_TIMESTAMP, b); + writer.write(new ImmutableBytesWritable(), kv); + writer.close(context); + writer = null; + FileSystem fs = dir.getFileSystem(conf); + RemoteIterator iterator = fs.listFiles(dir, true); + while (iterator.hasNext()) { + LocatedFileStatus keyFileStatus = iterator.next(); + HFile.Reader reader = + HFile.createReader(fs, keyFileStatus.getPath(), new CacheConfig(conf), conf); + assertEquals(reader.getCompressionAlgorithm().getName(), hfileoutputformatCompression); + } + } finally { + if (writer != null && context != null) { + writer.close(context); + } + dir.getFileSystem(conf).delete(dir, true); + } + + } + + } -- 1.8.3.1