Skip to content

Instantly share code, notes, and snippets.

@amalgjose
Created November 21, 2014 10:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalgjose/54eeb2bb39e9e23c32c3 to your computer and use it in GitHub Desktop.
Save amalgjose/54eeb2bb39e9e23c32c3 to your computer and use it in GitHub Desktop.
Java program to compress a file in snappy. This compressed file can be used in hadoop, because the libraries used in this program are taken from hadoop.
package com.snappy.codec;
/*
* @author : Amal G Jose
*
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.SnappyCodec;
import org.apache.hadoop.util.ReflectionUtils;
/*
*This program compresses the given file in snappy format
*
*/
public class CreateSnappy {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Enter <input> <output>");
System.exit(0);
}
try {
CompressionCodec codec = (CompressionCodec) ReflectionUtils
.newInstance(SnappyCodec.class, new Configuration());
OutputStream outStream = codec
.createOutputStream(new BufferedOutputStream(
new FileOutputStream(args[1])));
InputStream inStream = new BufferedInputStream(new FileInputStream(
args[0]));
int readCount = 0;
byte[] buffer = new byte[64 * 1024];
while ((readCount = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, readCount);
}
inStream.close();
outStream.close();
System.out.println("File Compressed");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment