All Products
Search
Document Center

Simple Log Service:Kompresi data

Last Updated:Jun 11, 2026

Mengirim volume besar data log melalui jaringan meningkatkan latensi dan biaya. Simple Log Service mendukung kompresi lz4 untuk operasi API tertentu, sehingga mengurangi lalu lintas jaringan dan meningkatkan throughput tanpa kehilangan data.

Kompres data permintaan

Operasi API berikut menerima data terkompresi lz4 dalam badan permintaan HTTP:

  • PutLogs (PutLogStoreLogs)

  • PutWebtracking

Untuk mengirim data terkompresi:

  1. Tambahkan x-log-compresstype: lz4 ke header permintaan HTTP.

  2. Kompres badan permintaan HTTP menggunakan lz4.

  3. Atur x-log-bodyrawsize dalam header permintaan ke ukuran badan sebelum dikompresi.

  4. Atur Content-Length dalam header permintaan ke ukuran badan setelah dikompresi.

Menerima data terkompresi

Operasi API PullLogs dapat mengembalikan data terkompresi lz4.

Untuk menerima data terkompresi:

  • Atur Accept-Encoding: lz4 dalam header permintaan. Server akan mengembalikan data terkompresi lz4.

  • Baca x-log-bodyrawsize dari header respons untuk mendapatkan ukuran badan sebelum dikompresi. Berikan nilai ini ke fungsi dekompresi Anda.

Contoh

  • Log mentah

    Contoh berikut menggunakan file log-sample.json sebagai input sampel. Ganti file ini dengan data log aktual Anda saat memanggil API.

    {
      "__tags__": {},
      "__topic__": "",
      "__source__": "47.100.XX.XX",
      "__logs__": [
        {
          "__time__": "03/22 08:51:01",
          "content": "*************** RSVP Agent started ***************",
          "method": "main",
          "level": "INFO"
        },
        {
          "__time__": "03/22 08:51:01",
          "content": "Specified configuration file: /u/user10/rsvpd1.conf",
          "method": "locate_configFile",
          "level": "INFO"
        },
        {
          "__time__": "03/22 08:51:01",
          "content": "Using log level 511",
          "method": "main",
          "level": "INFO"
        },
        {
          "__time__": "03/22 08:51:01",
          "content": "Get TCP images rc - EDC8112I Operation not supported on socket",
          "method": "settcpimage",
          "level": "INFO"
        },
        {
          "__time__": "03/22 08:51:01",
          "content": "Associate with TCP/IP image name = TCPCS",
          "method": "settcpimage",
          "level": "INFO"
        },
        {
          "__time__": "03/22 08:51:02",
          "content": "registering process with the system",
          "method": "reg_process",
          "level": "INFO"
        },
        {
          "__time__": "03/22 08:51:02",
          "content": "attempt OS/390 registration",
          "method": "reg_process",
          "level": "INFO"
        },
        {
          "__time__": "03/22 08:51:02",
          "content": "return from registration rc=0",
          "method": "reg_process",
          "level": "INFO"
        }
      ]
    }
  • Program uji

    Kode Python berikut mengompres file sampel dan mencetak rasio kompresi:

    from lz4 import block
    with open('log-sample.json', 'rb') as f:
        data = f.read()
        compressed = block.compress(data, store_size=False) # Kompres
        print(f'out/in: {len(compressed)}/{len(data)} Bytes')
        print(f'Rasio kompresi: {len(compressed)/len(data):.2%}')
  • Hasil kompresi

    Untuk file log-sample.json, rasio kompresinya adalah 39,30%. Hasil aktual bervariasi tergantung konten—data dengan lebih banyak pengulangan akan terkompresi lebih efisien.

    out/in: 542/1379 Bytes
    Rasio kompresi: 39,30%

Kode contoh

  • Contoh Go

    • Instal library dependensi

      go get github.com/pierrec/lz4
    • Kode contoh

      import (
          "fmt"
          "log"
      
          lz4 "github.com/cloudflare/golz4"
      )
      
      func main() {
          data := []byte("hello world, hello golang")
          // Kompres
          compressed := make([]byte, lz4.CompressBound(data))
          compressedSize, err := lz4.Compress(data, compressed)
          if err != nil {
              log.Fatal(err)
          }
          compressed = compressed[:compressedSize]
      
          // Dekompres
          bodyRawSize := len(data) // Untuk mendekompresi data dari Simple Log Service, baca x-log-bodyrawsize dari header HTTP yang dikembalikan.
          decompressed := make([]byte, bodyRawSize)
          err = lz4.Uncompress(compressed, decompressed)
          if err != nil {
              log.Fatal(err)
          }
          decompressed = decompressed[:bodyRawSize]
      }
  • Contoh Python

    • Instal library dependensi

      python3 -m pip install lz4
    • Kode contoh

      from lz4 import block
      
      data = b'hello world, hello sls'
      # Kompres
      compressed = block.compress(data, store_size=False)
      
      # Dekompres
      body_raw_size=len(data) # Saat mendekompresi data dari Simple Log Service, baca x-log-bodyrawsize dari header HTTP yang dikembalikan.
      decompressed = block.decompress(compressed, uncompressed_size=body_raw_size)
  • Contoh Java

    • Tambahkan dependensi Maven

      <dependency>
        <groupId>net.jpountz.lz4</groupId>
        <artifactId>lz4</artifactId>
        <version>1.3.0</version>
      </dependency>
    • Kode contoh

      package sample;
      
      import net.jpountz.lz4.LZ4Compressor;
      import net.jpountz.lz4.LZ4Factory;
      import net.jpountz.lz4.LZ4FastDecompressor;
      
      import java.io.File;
      import java.io.IOException;
      import java.nio.file.Files;
      
      public class Sample {
          public static void main(String[] args) throws IOException {
              byte[] data = "hello world, hello sls".getBytes();
              // Kompres
              LZ4Compressor compressor = LZ4Factory.fastestInstance().fastCompressor();
              int maxLen = compressor.maxCompressedLength(data.length);
              byte[] buffer = new byte[maxLen];
              int compressedSize = compressor.compress(data, 0, data.length, buffer, 0, maxLen);
              // Salin data buffer ke compressed.
              byte[] compressed = new byte[compressedSize];
              System.arraycopy(buffer, 0, compressed, 0, compressedSize);
      
              // Dekompres
              int bodyRawSize = data.length; // Saat mendekompresi data dari Simple Log Service, baca x-log-bodyrawsize dari header HTTP yang dikembalikan.
              LZ4FastDecompressor decompressor = LZ4Factory.fastestInstance().fastDecompressor();
              byte[] decompressed = new byte[bodyRawSize];
              decompressor.decompress(compressed, 0, decompressed, 0, bodyRawSize);
          }
      }
  • Contoh JavaScript

    • Gunakan npm atau yarn untuk menginstal library dependensi buffer dan lz4.

      npm install buffer lz4
    • Kode contoh

      import lz4 from 'lz4'
      import { Buffer } from 'buffer'
      
      // Kompres
      const data = 'hello world, hello sls'
      const output = Buffer.alloc(lz4.encodeBound(data.length))
      const compressedSize = lz4.encodeBlock(Buffer.from(data), output)
      const compressed = Uint8Array.prototype.slice.call(output, 0, compressedSize)
      
      // Dekompres
      const bodyRawSize = data.length; // Saat mendekompresi data dari Simple Log Service, baca x-log-bodyrawsize dari header HTTP yang dikembalikan.
      const decompressed = Buffer.alloc(bodyRawSize)
      lz4.decodeBlock(Buffer.from(compressed), decompressed)
      const result = decompressed.toString()
  • Contoh C++

    • Salin folder lz4 dan lib dari direktori root repositori SDK C++ ke folder tujuan Anda. Saat mengompilasi, tambahkan path pencarian library lz4 (misalnya, -L./lib) dan tautkan ke lz4 (misalnya, -llz4). Untuk informasi selengkapnya, lihat Instal SDK C++.

      g++ -o your_program your_program.cpp -std=gnu++11 -llz4 -L./lib/
    • Kode contoh

      #include "lz4/lz4.h"
      #include <string>
      #include <iostream>
      using namespace std;
      
      int main()
      {
          string data = "hello sls, hello lz4";
          // Kompres
          string compressed;
          compressed.resize(LZ4_compressBound(data.size()));
          int compressed_size = LZ4_compress(data.c_str(), &compressed[0], data.size());
          compressed.resize(compressed_size);
      
          // Dekompres
          string decompressed;
          int bodyRawSize = data.size();
          decompressed.resize(bodyRawSize);
          LZ4_decompress_safe(compressed.c_str(), &decompressed[0], compressed.size(), bodyRawSize);
          cout << decompressed << endl;
          return 0;
      }