Created
August 4, 2008 06:07
-
-
Save qingfeng/3863 to your computer and use it in GitHub Desktop.
Python Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
from __future__ import with_statement | |
import sys | |
import MySQLdb | |
class Log2DB(object): | |
"""hadoop log result into database""" | |
def __init__(self): | |
self.DB_USER='root' | |
self.DB_PASS='' | |
self.DB_HOST='localhost' | |
self.DB_NAME='historydb' | |
self.DB_TABLE_NAME='report_zxproduct_30' | |
self.DB_FIELD_NAME='idc,sys,savetime,traff_in,traff_out' | |
def gensql(self): | |
values=','.join( | |
map( lambda x:"%s",self.DB_FIELD_NAME.split(",") ) | |
) | |
sql='''INSERT INTO %s | |
(%s) | |
VALUES (%s) | |
'''%(self.DB_TABLE_NAME,self.DB_FIELD_NAME,values) | |
return sql | |
def run(self,stdin): | |
conn = MySQLdb.connect(user=self.DB_USER, | |
passwd=self.DB_PASS, | |
host=self.DB_HOST, | |
db=self.DB_NAME) | |
with conn as cursor: | |
cursor.execute("SET NAMES 'utf8'") | |
for r in stdin: | |
r=r.strip() | |
print r | |
data=tuple(r.split(",")) | |
sql=self.gensql() | |
# cursor.execute(sql,data) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
import sys | |
from logtodb import Log2DB | |
DB_FIELD_NAME='idc1,sys1,savetime1,traff_in1,traff_out' | |
if __name__=='__main__': | |
log=Log2DB() | |
log.DB_FIELD_NAME=DB_FIELD_NAME | |
log.run(sys.stdin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment