ITPub博客

首页 > 大数据 > 数据挖掘 > 日志处理

日志处理

原创 数据挖掘 作者:Noycezhang 时间:2014-12-13 15:44:23 0 删除 编辑
目的:
1、根据文本日志的每行的关键字,对日志进行拆分
2、把拆分后的日志文件,加载到mysql数据库

源码LogDeal.py:
# -*- coding: utf-8 -*- 
import MySQLdb,datetime


#获取当前时间的上一小时
now = datetime.datetime.now()
beforeHour = (now + datetime.timedelta(hours=-1)).strftime("%Y%m%d%H")
print beforeHour


fileDictionary = {}
f = open('log.txt', 'r')
for i in f.readlines():
    fileKey = 'tb' + i.split('|')[0]
    if fileDictionary.has_key(fileKey):
        print u'发现:' + fileKey
    else:
        print u'not find:' + fileKey
        fileName = fileKey + beforeHour
        outFile = open(fileName, 'a')
        fileDictionary[fileKey] = outFile
    #fileDictionary[fileKey].tell()
    #i += '\n'
    fileDictionary[fileKey].write(i)
        
#关闭读文件
f.close()


#关闭写文件
outFiles = fileDictionary.values()
for outFile in outFiles:
    outFile.write('\n')
    outFile.close()


#log文件入库到mysql
tableNames = fileDictionary.keys()
for tableName in tableNames:
    try:
        print tableName
        conn = MySQLdb.connect(host='192.168.1.81', user='noyce', passwd='123', db='test', port=3306)
        cur = conn.cursor()
        sql = "load data local infile '" + tableName + beforeHour + "' into table " + tableName + " fields terminated by '|'";
        print sql
        cur.execute(sql)
        conn.commit()
        cur.close()
        conn.close()
    except MySQLdb.Error, e:  
        print "Mysql Error %d: %s" % (e.args[0], e.args[1])



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23437254/viewspace-1366686/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2010-03-03

  • 博文量
    1
  • 访问量
    1944