You can set the compression method to snappy for logs that Log Service ships to Object Storage Service (OSS). Then, you can use the C++ library, Java library, Python library, and decompression tool for Linux to decompress these logs.

If you use the snappy algorithm to compress the logs to be shipped to OSS, these logs can occupy less bucket storage space in OSS. This topic describes the following methods for decompressing a .snappy file.

C++ library

Download the C++ library from the Snappy official website and use the Snappy.Uncompress method to decompress a .snappy file.

Java library

Download xerial snappy-java and use the Snappy.Uncompress or Snappy.SnappyInputStream method to decompress a .snappy file. The SnappyFramedInputStream method is not supported.

Note Due to an existing bug, snappy-java 1.1.2.1 may fail to decompress some .snappy files. This bug has been fixed in snappy-java 1.1.2.6 and later versions. We recommend that you use the latest Java library.
<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));
    }

Python library

  1. Download and install python-snappy.
  2. Run the decompression code.
    The sample decompression code is as follows:
    import snappy
    compressed = open('/tmp/temp.snappy').read()
    snappy.uncompress(compressed)
    
Note The following commands cannot be used to decompress a .snappy file shipped to OSS. The two commands can be used only in Hadoop mode (hadoop_stream_decompress) or stream mode (stream_decompress).
$ python -m snappy -c uncompressed_file compressed_file.snappy
$ python -m snappy -d compressed_file.snappy uncompressed_file

Decompression tool for Linux

In the Linux environment, Log Service also provides a tool to decompress a .snappy file. Click to download snappy_tool.

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