LZ4は可逆データ圧縮アルゴリズムであり、高効率でデータを圧縮および解凍するために使用されます。 Simple Log Serviceの一部のAPI操作はLZ4をサポートしています。 LZ4を使用してデータを圧縮すると、ネットワークトラフィックを削減し、トラフィックコストを削減し、Simple Log Service APIへのアクセス速度を向上させることができます。
HTTPリクエストボディの圧縮
次のAPIを呼び出すと、LZ4を使用してHTTPリクエストボディを圧縮できます。
LZ4を使用してHTTPリクエスト本文を圧縮するには、次の手順を実行します。
HTTPリクエストヘッダーにx-log-compressype: lz4を追加します。
LZ4を使用してHTTPリクエスト本文を圧縮します。
HTTPリクエストヘッダーのx-log-bodyrawsizeパラメーターをリクエスト本文の元のサイズに設定します。
HTTPリクエストヘッダーのContent-Lengthパラメーターをリクエスト本文の圧縮サイズに設定します。
レスポンスを圧縮するCompress a response
PullLogs操作を呼び出すと、LZ4を使用して応答を圧縮できます。
方法:
例
生ログ
log-sample.jsonファイルを例に挙げます。 以下は参考例です。 リクエストとレスポンスに含まれる情報は、Simple log Serviceの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"
}
]
}
テストの実行手順
次のPythonコードは、データ圧縮の手順を示しています。
lz4インポートブロックからの
from lz4 import block
with open('log-sample.json', 'rb') as f:
data = f.read()
compressed=block.compress (data, store_size=False) # Compress data:
print(f'out/in: {len(compressed)}/{len(data)} Bytes')
print(f'Compression ratio: {len(compressed)/len(data):.2%}')
結果
log-sample.jsonファイルの圧缩结果が返されます。 圧縮比は39.30% である。 圧縮率は、データの内容に依存する。 データに多数の繰り返しコンテンツが含まれている場合、圧縮率は高くなります。
out/in: 542/1379 Bytes
Compression ratio: 39.30%
サンプルコード
行く
依存ライブラリをインストールする:
go get github.com/pierrec/lz4
サンプルコード:
import (
"fmt"
"log"
lz4 "github.com/cloudflare/golz4"
)
func main() {
data := []byte("hello world, hello golang")
// Compress data.
compressed := make([]byte, lz4.CompressBound(data))
compressedSize, err := lz4.Compress(data, compressed)
if err != nil {
log.Fatal(err)
}
compressed = compressed[:compressedSize]
// Decompress data.
bodyRawSize := len(data) // You can use the x-log-bodyrawsize parameter in the returned HTTP request header to decompress data.
decompressed := make([]byte, bodyRawSize)
err = lz4.Uncompress(compressed, decompressed)
if err != nil {
log.Fatal(err)
}
decompressed = decompressed[:bodyRawSize]
}
Python
依存ライブラリをインストールする:
python3 -m pip install lz4
サンプルコード:
lz4インポートブロックからの
from lz4 import block
data = b'hello world, hello sls'
# Compress data.
compressed = block.compress(data, store_size=False)
# Decompress data.
body_raw_size=len(data) # You can use the x-log-bodyrawsize parameter in the returned HTTP request header to decompress data.
decompressed = block.decompress(compressed, uncompressed_size=body_raw_size)
Java
Mavenの依存関係を追加:
<dependency>
<groupId>net.jpountz.lz4</groupId>
<artifactId>lz4</artifactId>
<version>1.3.0</version>
</dependency>
サンプルコード:
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();
// Compress data.
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);
// Copy the buffer to the compressed parameter.
byte[] compressed = new byte[compressedSize];
System.arraycopy(buffer, 0, compressed, 0, compressedSize);
// Decompress data.
int bodyRawSize = data.length; // You can use the x-log-bodyrawsize parameter in the returned HTTP request header to decompress data.
LZ4FastDecompressor decompressor = LZ4Factory.fastestInstance().fastDecompressor();
byte[] decompressed = new byte[bodyRawSize];
decompressor.decompress(compressed, 0, decompressed, 0, bodyRawSize);
}
}
JavaScript
npmまたはYarnを使用して、バッファおよびlz4依存ライブラリをインストールします。
npm install buffer lz4
サンプルコード:
import lz4 from 'lz4'
import { Buffer } from 'buffer'
// Compress data.
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)
// Decompress data.
const bodyRawSize = data.length; // You can use the x-log-bodyrawsize parameter in the returned HTTP request header to decompress data.
const decompressed = Buffer.alloc(bodyRawSize)
lz4.decodeBlock(Buffer.from(compressed), decompressed)
const result = decompressed.toString()
C ++
C ++ SDKプロジェクトのルートディレクトリからlz4ディレクトリとlibディレクトリをコピー先ディレクトリにコピーします。 コードをコンパイルするときに、コンパイルパラメーターを追加し、lz4依存関係ライブラリのパスとリンクを追加します。 たとえば、パス-L./libとリンク-llz4を追加できます。 詳細については、「Simple Log Service SDK For C ++ のインストール」をご参照ください。
g++ -o your_progame your_progame.cpp -std=gnu++11 -llz4 -L./lib/
サンプルコード:
#include "lz4/lz4.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
string data = "hello sls, hello lz4";
// Compress data.
string compressed;
compressed.resize(LZ4_compressBound(data.size()));
int compressed_size = LZ4_compress(data.c_str(), &compressed[0], data.size());
compressed.resize(compressed_size);
// Decompress data.
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;
}