Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ class BeamModulePlugin implements Plugin<Project> {
vendored_grpc_1_13_1 : "org.apache.beam:beam-vendor-grpc-1_13_1:0.2",
vendored_guava_20_0 : "org.apache.beam:beam-vendor-guava-20_0:0.1",
woodstox_core_asl : "org.codehaus.woodstox:woodstox-core-asl:4.4.1",
zstd_jni : "com.github.luben:zstd-jni:1.3.7-3",
quickcheck_core : "com.pholser:junit-quickcheck-core:$quickcheck_version",
],
groovy: [
Expand Down
1 change: 1 addition & 0 deletions sdks/java/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {
shadow library.java.slf4j_api
shadow library.java.avro
shadow library.java.snappy_java
shadow library.java.zstd_jni
shadow library.java.joda_time
shadow "org.tukaani:xz:1.8"
provided library.java.junit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@
* }</pre>
*
* <p>Supported compression algorithms are {@link Compression#GZIP}, {@link Compression#BZIP2},
* {@link Compression#ZIP} and {@link Compression#DEFLATE}. User-defined compression types are
* supported by implementing a {@link DecompressingChannelFactory}.
* {@link Compression#ZIP}, {@link Compression#ZSTD}, and {@link Compression#DEFLATE}. User-defined
* compression types are supported by implementing a {@link DecompressingChannelFactory}.
*
* <p>By default, the compression algorithm is selected from those supported in {@link Compression}
* based on the file name provided to the source, namely {@code ".bz2"} indicates {@link
* Compression#BZIP2}, {@code ".gz"} indicates {@link Compression#GZIP}, {@code ".zip"} indicates
* {@link Compression#ZIP} and {@code ".deflate"} indicates {@link Compression#DEFLATE}. If the file
* name does not match any of the supported algorithms, it is assumed to be uncompressed data.
* {@link Compression#ZIP}, {@code ".zst"} indicates {@link Compression#ZSTD}, and {@code
* ".deflate"} indicates {@link Compression#DEFLATE}. If the file name does not match any of the
* supported algorithms, it is assumed to be uncompressed data.
*
* @param <T> The type to read from the compressed file.
*/
Expand Down Expand Up @@ -87,6 +88,9 @@ public enum CompressionMode implements DecompressingChannelFactory {
/** @see Compression#ZIP */
ZIP(Compression.ZIP),

/** @see Compression#ZSTD */
ZSTD(Compression.ZSTD),

/** @see Compression#DEFLATE */
DEFLATE(Compression.DEFLATE);

Expand Down Expand Up @@ -132,6 +136,9 @@ static DecompressingChannelFactory fromCanonical(Compression compression) {
case ZIP:
return ZIP;

case ZSTD:
return ZSTD;

case DEFLATE:
return DEFLATE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.apache.commons.compress.compressors.deflate.DeflateCompressorInputStream;
import org.apache.commons.compress.compressors.deflate.DeflateCompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.zstandard.ZstdCompressorInputStream;
import org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream;

/** Various compression types for reading/writing files. */
public enum Compression {
Expand Down Expand Up @@ -126,6 +128,24 @@ public WritableByteChannel writeCompressed(WritableByteChannel channel) throws I
}
},

/**
* ZStandard compression.
*
* <p>The {@code .zst} extension is specified in <a href=https://tools.ietf.org/html/rfc8478>RFC
* 8478</a>.
*/
ZSTD(".zst", ".zst", ".zstd") {
@Override
public ReadableByteChannel readDecompressed(ReadableByteChannel channel) throws IOException {
return Channels.newChannel(new ZstdCompressorInputStream(Channels.newInputStream(channel)));
}

@Override
public WritableByteChannel writeCompressed(WritableByteChannel channel) throws IOException {
return Channels.newChannel(new ZstdCompressorOutputStream(Channels.newOutputStream(channel)));
}
},

/** Deflate compression. */
DEFLATE(".deflate", ".deflate", ".zlib") {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ public enum CompressionType implements WritableByteChannelFactory {
/** @see Compression#BZIP2 */
BZIP2(Compression.BZIP2),

/** @see Compression#ZSTD */
ZSTD(Compression.ZSTD),

/** @see Compression#DEFLATE */
DEFLATE(Compression.DEFLATE);

Expand Down Expand Up @@ -183,6 +186,9 @@ public static CompressionType fromCanonical(Compression canonical) {
case ZIP:
throw new IllegalArgumentException("ZIP is unsupported");

case ZSTD:
return ZSTD;

case DEFLATE:
return DEFLATE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,10 @@ public enum CompressionType {
/** @see Compression#ZIP */
ZIP(Compression.ZIP),

/** @see Compression#ZIP */
/** @see Compression#ZSTD */
ZSTD(Compression.ZSTD),

/** @see Compression#DEFLATE */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

DEFLATE(Compression.DEFLATE);

private final Compression canonical;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.apache.commons.compress.compressors.deflate.DeflateCompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream;
import org.joda.time.Instant;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -113,6 +114,14 @@ public void testAutoSplittable() throws Exception {
source = CompressedSource.from(new ByteSource("input.ZIP", 1));
assertFalse(source.isSplittable());

// ZSTD files are not splittable
source = CompressedSource.from(new ByteSource("input.zst", 1));
assertFalse(source.isSplittable());
source = CompressedSource.from(new ByteSource("input.ZST", 1));
assertFalse(source.isSplittable());
source = CompressedSource.from(new ByteSource("input.zstd", 1));
assertFalse(source.isSplittable());

// DEFLATE files are not splittable
source = CompressedSource.from(new ByteSource("input.deflate", 1));
assertFalse(source.isSplittable());
Expand Down Expand Up @@ -180,6 +189,13 @@ public void testEmptyReadGzip() throws Exception {
runReadTest(input, CompressionMode.GZIP);
}

/** Test reading empty input with zstd. */
@Test
public void testEmptyReadZstd() throws Exception {
byte[] input = generateInput(0);
runReadTest(input, CompressionMode.ZSTD);
}

private static byte[] compressGzip(byte[] input) throws IOException {
ByteArrayOutputStream res = new ByteArrayOutputStream();
try (GZIPOutputStream gzipStream = new GZIPOutputStream(res)) {
Expand Down Expand Up @@ -258,7 +274,14 @@ public void testCompressedReadBzip2() throws Exception {
runReadTest(input, CompressionMode.BZIP2);
}

/** Test reading according to filepattern when the file is bzipped. */
/** Test reading empty input with zstd. */
@Test
public void testCompressedReadZstd() throws Exception {
byte[] input = generateInput(0);
runReadTest(input, CompressionMode.ZSTD);
}

/** Test reading according to filepattern when the file is gzipped. */
@Test
public void testCompressedAccordingToFilepatternGzip() throws Exception {
byte[] input = generateInput(100);
Expand All @@ -267,7 +290,7 @@ public void testCompressedAccordingToFilepatternGzip() throws Exception {
verifyReadContents(input, tmpFile, null /* default auto decompression factory */);
}

/** Test reading according to filepattern when the file is gzipped. */
/** Test reading according to filepattern when the file is bzipped. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other good catch good.

@Test
public void testCompressedAccordingToFilepatternBzip2() throws Exception {
byte[] input = generateInput(100);
Expand All @@ -276,6 +299,15 @@ public void testCompressedAccordingToFilepatternBzip2() throws Exception {
verifyReadContents(input, tmpFile, null /* default auto decompression factory */);
}

/** Test reading according to filepattern when the file is zstd compressed. */
@Test
public void testCompressedAccordingToFilepatternZstd() throws Exception {
byte[] input = generateInput(100);
File tmpFile = tmpFolder.newFile("test.zst");
writeFile(tmpFile, input, CompressionMode.ZSTD);
verifyReadContents(input, tmpFile, null /* default auto decompression factory */);
}

/** Test reading multiple files with different compression. */
@Test
public void testHeterogeneousCompression() throws Exception {
Expand All @@ -301,6 +333,11 @@ public void testHeterogeneousCompression() throws Exception {
writeFile(bzip2File, generated, CompressionMode.BZIP2);
expected.addAll(Bytes.asList(generated));

File zstdFile = tmpFolder.newFile(baseName + ".zst");
generated = generateInput(1000, 4);
writeFile(zstdFile, generated, CompressionMode.ZSTD);
expected.addAll(Bytes.asList(generated));

String filePattern = new File(tmpFolder.getRoot().toString(), baseName + ".*").toString();

CompressedSource<Byte> source = CompressedSource.from(new ByteSource(filePattern, 1));
Expand Down Expand Up @@ -359,6 +396,18 @@ public void testBzip2FileIsNotSplittable() throws Exception {
assertFalse(source.isSplittable());
}

@Test
public void testZstdFileIsNotSplittable() throws Exception {
String baseName = "test-input";

File compressedFile = tmpFolder.newFile(baseName + ".zst");
writeFile(compressedFile, generateInput(10), CompressionMode.ZSTD);

CompressedSource<Byte> source =
CompressedSource.from(new ByteSource(compressedFile.getPath(), 1));
assertFalse(source.isSplittable());
}

/**
* Test reading an uncompressed file with {@link CompressionMode#GZIP}, since we must support this
* due to properties of services that we read from.
Expand All @@ -383,6 +432,16 @@ public void testFalseBzip2Stream() throws Exception {
verifyReadContents(input, tmpFile, CompressionMode.BZIP2);
}

/** Test reading an uncompressed file with {@link Compression#ZSTD}, and show that we fail. */
@Test
public void testFalseZstdStream() throws Exception {
byte[] input = generateInput(1000);
File tmpFile = tmpFolder.newFile("test.zst");
Files.write(input, tmpFile);
thrown.expectMessage("Decompression error: Unknown frame descriptor");
verifyReadContents(input, tmpFile, CompressionMode.ZSTD);
}

/**
* Test reading an empty input file with gzip; it must be interpreted as uncompressed because the
* gzip header is two bytes.
Expand Down Expand Up @@ -476,6 +535,8 @@ private OutputStream getOutputStreamForMode(CompressionMode mode, OutputStream s
return new BZip2CompressorOutputStream(stream);
case ZIP:
return new TestZipOutputStream(stream);
case ZSTD:
return new ZstdCompressorOutputStream(stream);
case DEFLATE:
return new DeflateCompressorOutputStream(stream);
default:
Expand Down