def log2db(logfile): dbutils.execute_commit_sql('DELETE FROM accesslog;') fhandler = open(logfile, 'r') rt_dict = {} # 统计 while True: line = fhandler.readline() if line == '': break nodes = line.split() ip, url, code = nodes[0], nodes[6], nodes[8] key = (ip, url, code) if key not in rt_dict: rt_dict[key] = 1 else: rt_dict[key] = rt_dict[key] + 1 fhandler.close() rt_list = [] for _key, _cnt in rt_dict.items(): rt_list.append(_key + (_cnt, )) _sql = 'insert into accesslog(ip, url, code, cnt) values (%s, %s, %s, %s)' dbutils.bulker_commit_sql(_sql, rt_list)
def log2db(logfile): dbutils.execute_commit_sql('delete from accesslog') handler = open(logfile,'r') rt_dict = {} rt_list = [] #统计 _sql = 'insert into accesslog(ip,url,code,cnt) values (%s,%s,%s,%s)' while True: line = handler.readline() if line == "": break nodes = line.split() ip,url,code = nodes[0],nodes[6],nodes[8] key = (ip,url,code) if key not in rt_dict: rt_dict[key] = 1 else: rt_dict[key] = rt_dict[key] + 1 handler.close() for _key,_cnt in rt_dict.items(): rt_list.append(_key + (_cnt,)) dbutils.bulker_commit_sql(_sql,rt_list)
def logtodb(logfile): fhandler = open(logfile,'r') rt_dict = {} _sql = 'insert into accesslog(count, ip, url, code) values (%s, %s, %s, %s)' while True: line = fhandler.readline() if line == '': break nodes = line.split() ip , url ,code = nodes[0],nodes[6],nodes[8] key = (ip,url,code) if key not in rt_dict: rt_dict[key] = 1 else: rt_dict[key] += 1 #rt_list.append((nodes[6],nodes[0],nodes[8])) fhandler.close() # 排序 rt_list = rt_dict.items() # [(key,value),(key,value)] for j in range(0, 10): for i in range(0, len(rt_list) - 1): if rt_list[i][1] > rt_list[i + 1][1]: temp = rt_list[i] rt_list[i] = rt_list[i + 1] rt_list[i + 1] = temp db_list = [] for node in rt_list[-1:-11:-1]: db_list.append((node[1],node[0][0],node[0][1],node[0][2])) print db_list dbutils.bulker_commit_sql(_sql, db_list)
def log2db(logfile): fhandler = open(logfile, 'r') rt_dict = {} Flag = True while Flag: line = fhandler.readline() if line == '': break nodes = line.split() ip, url, code = nodes[0], nodes[6], nodes[8] key = (ip, url, code) if key not in rt_dict: rt_dict[key] = 1 else: rt_dict[key] += 1 fhandler.close() rt_list = [] count = 0 for _key, _cnt in rt_dict.items(): count += 1 rt_list.append(_key + (_cnt, )) if count == 100: break _sql = 'insert into log(ip,url,code,count) values(%s,%s,%s,%s)' dbutils.bulker_commit_sql(_sql, rt_list)
def log2db(logfile): fhandler = open(logfile, 'r') rt_list = [] _sql = 'insert into accesslog(url,ip,code) values (%s,%s,%s)' #统计 while True: line = fhandler.readline() if line == '': break nodes = line.split() rt_list.append((nodes[0], nodes[6], nodes[8])) fhandler.close() dbutils.bulker_commit_sql(_sql, rt_list)
def loganalysis(logfile): execute_commit_sql('delete from accesslog;') fhandler = open(logfile, 'rb') rt = {} while True: line = fhandler.readline() if line == "": break nodes = line.split() ip, url, code = nodes[0], nodes[6], nodes[8] key = (ip, url, code) if key not in rt: rt[key] = 1 else: rt[key] += 1 rt_list = rt.items() rt_data = [] for line in rt_list: lines = line[0] + (line[1], ) rt_data.append(lines) _sql = "insert into accesslog(ip,url,code,cnt) VALUES (%s,%s,%s,%s)" bulker_commit_sql(_sql, rt_data)