Hadoop detailed (eight) codec

Source: Internet
Author: User
Tags command line

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.