Skip to content

Commit

Permalink
Merge pull request #26 from wjo1212/master
Browse files Browse the repository at this point in the history
support to compress data for get/put API with zip
  • Loading branch information
wjo1212 committed Nov 20, 2017
2 parents 8294af2 + f74f66d commit 52e3847
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
16 changes: 12 additions & 4 deletions aliyun/log/logclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from .version import API_VERSION, USER_AGENT
import json
import six
import zlib

CONNECTION_TIME_OUT = 20

Expand Down Expand Up @@ -264,8 +265,9 @@ def put_logs(self, request):

compress_data = None
if is_compress:
headers['x-log-compresstype'] = 'lz4'
compress_data = logservice_lz4.compress(body)
headers['x-log-compresstype'] = 'deflate'
compress_data = zlib.compress(body)
#compress_data = logservice_lz4.compress(body)

params = {}
logstore = request.get_logstore()
Expand Down Expand Up @@ -566,7 +568,7 @@ def pull_logs(self, project_name, logstore_name, shard_id, cursor, count=1000, e
"""
headers = {}
if compress:
headers['Accept-Encoding'] = 'lz4'
headers['Accept-Encoding'] = 'gzip'
else:
headers['Accept-Encoding'] = ''

Expand All @@ -580,10 +582,16 @@ def pull_logs(self, project_name, logstore_name, shard_id, cursor, count=1000, e
if end_cursor:
params['end_cursor'] = end_cursor
(resp, header) = self._send("GET", project_name, None, resource, params, headers, "binary")
if compress:

compress_type = Util.h_v_td(header,'x-log-compresstype', '').lower()
if compress_type == 'lz4':
raw_size = int(Util.h_v_t(header, 'x-log-bodyrawsize'))
raw_data = logservice_lz4.uncompress(raw_size, resp)
return PullLogResponse(raw_data, header)
elif compress_type in ('gzip', 'deflate'):
raw_size = int(Util.h_v_t(header, 'x-log-bodyrawsize'))
raw_data = zlib.decompress(resp)
return PullLogResponse(raw_data, header)
else:
return PullLogResponse(resp, header)

Expand Down
10 changes: 7 additions & 3 deletions aliyun/log/putlogsrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ class PutLogsRequest(LogRequest):
:type logitems: list<LogItem>
:param logitems: log data
:type logtags: list
:param logtags: list of key:value tag pair , [(tag_key_1,tag_value_1) , (tag_key_2,tag_value_2)]
:type hashKey: String
:param hashKey: put data with set hash, the data will be send to shard whose range contains the hashKey
:type compress: bool
:param compress: if need to compress the logs
:type logtags: list
:param logtags: list of key:value tag pair , [(tag_key_1,tag_value_1) , (tag_key_2,tag_value_2)]
"""

def __init__(self, project=None, logstore=None, topic=None, source=None, logitems=None, hashKey=None,
Expand Down
2 changes: 1 addition & 1 deletion aliyun/log/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.6.9'
__version__ = '0.6.10'
USER_AGENT = 'log-python-sdk-v-' + __version__
API_VERSION = '0.6.0'

Expand Down
10 changes: 6 additions & 4 deletions tests/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os


def sample_put_logs(client, project, logstore):
def sample_put_logs(client, project, logstore, compress=False):
topic = 'TestTopic_2'
source = ''
contents = [
Expand All @@ -21,7 +21,7 @@ def sample_put_logs(client, project, logstore):
logItem.set_contents(contents)
for i in range(0, 1):
logitemList.append(logItem)
request = PutLogsRequest(project, logstore, topic, source, logitemList)
request = PutLogsRequest(project, logstore, topic, source, logitemList, compress=compress)

response = client.put_logs(request)
response.log_print()
Expand All @@ -39,12 +39,12 @@ def sample_put_logs(client, project, logstore):
res.log_print()

# @log_enter_exit
def sample_pull_logs(client, project, logstore):
def sample_pull_logs(client, project, logstore, compress=False):
res = client.get_cursor(project, logstore, 0, int(time.time() - 60))
res.log_print()
cursor = res.get_cursor()

res = client.pull_logs(project, logstore, 0, cursor, 1)
res = client.pull_logs(project, logstore, 0, cursor, 1, compress=compress)
res.log_print()

# @log_enter_exit
Expand Down Expand Up @@ -258,8 +258,10 @@ def main():
sample_list_topics(client, project, logstore)
time.sleep(40)
sample_put_logs(client, project, logstore)
sample_put_logs(client, project, logstore, compress=True)

sample_pull_logs(client, project, logstore)
sample_pull_logs(client, project, logstore, compress=True)

time.sleep(40)
sample_get_logs(client, project, logstore)
Expand Down

0 comments on commit 52e3847

Please sign in to comment.