Created
November 21, 2014 10:48
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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