lz4是一种无损压缩算法,具有高速解码与压缩能力。日志服务部分API接口支持lz4压缩算法,使用lz4压缩算法可以减少网络传输流量,降低流量费用,提升接口访问速度。
压缩请求数据
日志服务如下API接口支持在HTTP请求体中传输lz4压缩格式的数据。
PutLogs(PutLogStoreLogs)
PutWebtracking
其使用方法主要分为如下几个步骤:
在HTTP请求头中添加
x-log-compresstype: lz4
。使用lz4压缩算法压缩HTTP请求体。
将HTTP请求头中的
x-log-bodyrawsize
设为请求体压缩前大小。将HTTP请求头中的
Content-Length
设为请求体压缩后大小。
接收压缩数据
日志服务的PullLogs接口可返回lz4压缩格式数据。
使用方法:
通过在请求头中设置
Accept-Encoding: lz4
,服务端将会返回lz4压缩数据。返回的请求头中
x-log-bodyrawsize
标识了请求体的压缩前的原始大小,可作为解压参数使用。
使用示例
原始日志
以log-sample.json的内容作为参考示例。在实际使用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代码为例,其压缩过程如下:
from lz4 import block with open('log-sample.json', 'rb') as f: data = f.read() compressed = block.compress(data, store_size=False) # 压缩 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%
示例代码
该文章对您有帮助吗?
- 本页导读 (1)
- 压缩请求数据
- 接收压缩数据
- 使用示例
- 示例代码