import datetime
import sys
import getopt
import hashlib
from elasticsearch import Elasticsearch
"""
初始化elasticsearch连接
"""
def init_es():
return Elasticsearch(["localhost:9200"])
"""
查询数据, 支持分页
"""
def query_data(log_date, puid, data_rows, page_num):
es = init_es()
body = {
"query":{
"term":{
"puid":""
}
},
"sort":[{
"datatime":{
"order":"asc"
}
},{
"@timestamp":{
"order":"asc"
}
}],
"size":"",
"from":""
}
index_name = "test-"+log_date
body["query"]["term"]["puid"] = run_md5(str(puid))
body["size"] = data_rows
from_page = int(data_rows) * (int(page_num)-1)
body["from"] = from_page
data_array = es.search(index=index_name, doc_type='doc', body=body)
print_data(data_array, data_rows)
"""
打印数据
"""
def print_data(data_array, data_rows):
datas = data_array["hits"]["hits"]
#print datas
print "符合条件的数据总条数为:" + str(data_array['hits']['total'])
print "当前显示的数据如下:"
for data in datas:
result = data['_source']['message']
print result.strip(result.split("\t")[0]).strip("\t")
"""
md5加密
"""
def run_md5(puid):
md = hashlib.md5()
md.update(puid.encode('utf-8'))
return md.hexdigest()
"""
处理逻辑调用查询
"""
def run(param):
puid = param['puid']
log_date = param['log_date'] if param['log_date'] else datetime.datetime.now().strftime('%Y-%m-%d')
data_rows = param['data_rows']
page_num = param['page_num']
query_data(log_date, puid, data_rows, page_num)
def main(argv):
try:
opts, args = getopt.getopt(argv[1:], 'hp:d:r:n:', ['help', 'puid=', 'log_date=', 'data_rows=', 'page_num='])
except getopt.GetoptError as err:
print str(err)
sys.exit(2)
if not opts:
print "The puid is a must !"
opts = [('-h', '')]
VARS = {'puid': None, 'log_date': None, 'data_rows': 50, 'page_num': 1}
for opt, value in opts:
if opt in ('-h', '--help'):
print("")
print("Usage:python query_client_log.py -p puid [-r data_rows -d log_date [-n page_num]] | --puid=puid ....")
print("-p, --puid 用户id,此为必传参数")
print("-d, --log_date 数据日期,格式:yyyy-mm-dd")
print("-r, --data_rows 查询数据的条数,如为分页查询则为每页数据的条数,默认50条")
print("-n, --page_num 分页查询,当前查询的页号,默认不分页")
print("-h, --help 查看帮助并退出")
print("")
sys.exit()
if opt in ('-p', '--puid'):
VARS['puid'] = value
elif opt in ('-d', '--log_date'):
VARS['log_date'] = value
elif opt in ('-r', '--data_rows'):
VARS['data_rows'] = value
elif opt in ('-n', '--page_num'):
VARS['page_num'] = value
run(VARS)
if __name__ == '__main__':
main(sys.argv)