Brief introduction
CODEC is actually an acronym for the prefix of the coder and decoder two words. Compressioncodec defines a compression and decompression interface, and the codec we're talking about is a class that implements some of the compressed formats of the Compressioncodec interface, and here's a list of these classes:
Using Compressioncodes to decompress
Compressioncodec has two methods to facilitate compression and decompression.
Compression: Compressionoutputstream object obtained by Createoutputstream (OutputStream out) method
Decompression: Obtains Compressioninputstream object via Createinputstream (InputStream in) method
Sample code for compression
Package com.sweetop.styhadoop;
Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.io.IOUtils;
Import Org.apache.hadoop.io.compress.CompressionCodec;
Import Org.apache.hadoop.io.compress.CompressionOutputStream;
Import Org.apache.hadoop.util.ReflectionUtils;
/** * Created with IntelliJ idea. * User:lastsweetop * date:13-6-25 * Time: PM 10:09 * To change this template use File | Settings |
File Templates. */public class Streamcompressor {public static void main (string[] args) throws Exception {String CODECC
Lassname = Args[0];
class<?> Codecclass = Class.forName (codecclassname);
Configuration conf = new Configuration ();
Compressioncodec codec = (COMPRESSIONCODEC) reflectionutils.newinstance (codecclass, conf);
Compressionoutputstream out = Codec.createoutputstream (System.out);
Ioutils.copybytes (system.in, out, 4096, false); Out.finish ();
}
}
Accept the parameters of a Compressioncodec implementation class from the command line, and then reflectionutils the class by instantiating it, invoking the Compressioncodec interface method to encapsulate the standard output stream into a compressed stream, The standard input stream is copied into the compressed stream through the Ioutils class Copybytes method, and the final method of Compressioncodec is called to complete the compression.