If you use Snappy to compress data when you ship data from Log Service to OSS, you can use tools to decompress the data. This topic describes the tools that you can use to decompress Snappy compressed OSS buckets. The decompression tools include Snappy decompressor for C++, Snappy decompressor for Java, Snappy decompressor for Python, and Linux-based Snappy decompressor.

Snappy decompressor for C++

Download the C++ library from the snappy page and use the Snappy.Uncompress method to decompress Snappy compressed OSS buckets.

Snappy decompressor for Java

Download the Java library from the xerial snappy-java page and use the Snappy.Uncompress or Snappy.SnappyInputStream method to decompress Snappy compressed OSS buckets. The SnappyFramedInputStream method is not supported.

Note If you use Snappy decompressor for Java 1.1.2.1, some Snappy compressed OSS buckets may fail to be decompressed. For information about the possible exceptions, visit Bad handling of the MAGIC HEADER. To avoid this issue, you can use Snappy decompressor for Java 1.1.2.6 or later.
<dependency>
<groupId>org.xerial.snappy</groupId>
<artifactId>snappy-java</artifactId>
<version>1.0.4.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
  • Snappy.Uncompress

    String fileName = "C:\\Downloads\\36_1474212963188600684_4451886.snappy";
    RandomAccessFile randomFile = new RandomAccessFile(fileName, "r");
    int fileLength = (int) randomFile.length();
    randomFile.seek(0);
    byte[] bytes = new byte[fileLength];
    int byteread = randomFile.read(bytes);
    System.out.println(fileLength);
    System.out.println(byteread);
    byte[] uncompressed = Snappy.uncompress(bytes);
    String result = new String(uncompressed, "UTF-8");
    System.out.println(result);
  • Snappy.SnappyInputStream

    String fileName = "C:\\Downloads\\36_1474212963188600684_4451886.snappy";
    SnappyInputStream sis = new SnappyInputStream(new FileInputStream(fileName));
    byte[] buffer = new byte[4096];
    int len = 0;
    while ((len = sis.read(buffer)) ! = -1) {
        System.out.println(new String(buffer, 0, len));
    }

Snappy decompressor for Python

  1. Download and install Snappy decompressor for Python.
  2. Run the decompression script.
    The following example is a sample decompression script:
    import snappy
    compressed = open('/tmp/temp.snappy').read()
    snappy.uncompress(compressed)
                                
    Note The following two commands cannot be used to decompress Snappy compressed OSS buckets. These commands can be used only in Hadoop mode (hadoop_stream_decompress) or streaming mode (stream_decompress).
    $ python -m snappy -c uncompressed_file compressed_file.snappy
    $ python -m snappy -d compressed_file.snappy uncompressed_file                            

Linux-based Snappy decompressor

Log Service allows you to decompress Snappy compressed files on a Linux-based server. Click snappy_tool to download the decompressor. Replace 03_1453457006548078722_44148.snappy and 03_1453457006548078722_44148 in the following code with the values specific to your environment and then run the following code:

./snappy_tool 03_1453457006548078722_44148.snappy 03_1453457006548078722_44148
compressed.size: 2217186
snappy::Uncompress return: 1
uncompressed.size: 25223660