From fc890a2ecb12c3c664ad00f8b2f8788dba04c71d Mon Sep 17 00:00:00 2001 From: Lars Hofhansl Date: Thu, 2 Jun 2016 12:19:02 -0700 Subject: [PATCH] HBASE-15881 Allow BZIP2 compression. --- .../hadoop/hbase/io/compress/Compression.java | 29 ++++++++++++++++++- .../hbase/util/TestCompressionTest.java | 2 ++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/compress/Compression.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/compress/Compression.java index 821b21fdd0f..6dc419006eb 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/io/compress/Compression.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/io/compress/Compression.java @@ -235,7 +235,34 @@ public final class Compression { throw new RuntimeException(e); } } - }; + }, + BZIP2("bzip2") { + // Use base type to avoid compile-time dependencies. + private volatile transient CompressionCodec bzipCodec; + private transient Object lock = new Object(); + + @Override + CompressionCodec getCodec(Configuration conf) { + if (bzipCodec == null) { + synchronized (lock) { + if (bzipCodec == null) { + bzipCodec = buildCodec(conf); + } + } + } + return bzipCodec; + } + + private CompressionCodec buildCodec(Configuration conf) { + try { + Class externalCodec = + getClassLoaderForCodec().loadClass("org.apache.hadoop.io.compress.BZip2Codec"); + return (CompressionCodec) ReflectionUtils.newInstance(externalCodec, conf); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + }; private final Configuration conf; private final String compressName; diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestCompressionTest.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestCompressionTest.java index 43c7cfc6e32..398f3f0dd88 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestCompressionTest.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestCompressionTest.java @@ -75,12 +75,14 @@ public class TestCompressionTest { nativeCodecTest("LZO", "lzo2", "com.hadoop.compression.lzo.LzoCodec"); nativeCodecTest("LZ4", null, "org.apache.hadoop.io.compress.Lz4Codec"); nativeCodecTest("SNAPPY", "snappy", "org.apache.hadoop.io.compress.SnappyCodec"); + nativeCodecTest("BZIP2", "bzip2", "org.apache.hadoop.io.compress.BZip2Codec"); } else { // Hadoop nativelib is not available LOG.debug("Native code not loaded"); assertFalse(CompressionTest.testCompression("LZO")); assertFalse(CompressionTest.testCompression("LZ4")); assertFalse(CompressionTest.testCompression("SNAPPY")); + assertFalse(CompressionTest.testCompression("BZIP2")); } }