Hadoop File compression and decompression

Source: Internet
Author: User

A simple test program for hadoop File compression and decompression:

Package Org. myorg; import Java. io. *; import Org. apache. hadoop. conf. configuration; import Org. apache. hadoop. io. compress. compressioncodec; import Org. apache. hadoop. io. compress. compressionoutputstream; import Org. apache. hadoop. util. reflectionutils; public class streamcompressor {public static void main (string [] ARGs) throws exception {string codecclassname = ARGs [0]; Class <?> Codecclass = Class. forname (codecclassname); configuration conf = new configuration (); compressioncodec codec = (compressioncodec) reflectionutils. newinstance (codecclass, conf); // compressionoutputstream out = codec. createoutputstream (New fileoutputstream (new file ("text"); string STR = "Try compress and decompress"; byte [] bytes = new byte [1024]; bytes = Str. getbytes (); // ioutils. copybytes (New bytearrayinputstream (bytes), Out, 4096, false); out. write (bytes); out. finish (); // extract the data from the text file and output it to the console inputstream in = codec. createinputstream (New fileinputstream (new file ("text"); bufferedinputstream bfin = new bufferedinputstream (in); bfin. read (bytes); system. out. println (new string (bytes ));}}

1. The ARG [0] value is org. Apache. hadoop. Io. Compress. gzipcodec.

2. first create a compressed output stream out, write data to the output stream (try compress and decompress), and then close the output stream. At this time, the data is compressed into the text file.

3. Extract the input stream from the text file and output it. The output result is as follows. Some warning information is not clear yet.

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.