Yelp / pyleus

Pyleus is a Python framework for developing and launching Storm topologies.
Apache License 2.0
403 stars 107 forks source link

How can I write result into MySQL? #179

Open ghost opened 8 years ago

ghost commented 8 years ago

I submited the jar packege,topology can be normal operation. but can't write the result into MySQL and can get the log.I have already create a table in my database called result `#coding=utf-8 import MySQLdb import logging

from pyleus.storm import SimpleBolt

log = logging.getLogger('log_results')

def write_result(freqset,count): conn = MySQLdb.connect(host='10.1.1.5',user='root',passwd='',db='datamining',port=3306) cur = conn.cursor() st = '' for i in freqset: st = st + i + ',' sql = "select * from result where freqset = " + "'"+ st +"'" cur.execute(sql) returned_value = cur.fetchone() if returned_value != None: supnum = returned_value[1]+count sql1 = "update result set supnum = %s where freqset = '%s';" %(str(supnum),st) cur.execute(sql1) conn.commit else: value = [st,count] sql2 = 'insert into result values(%s,%s)' cur.execute(sql2,value) conn.commit()

cur.close()
conn.close()

class LogResultsBolt(SimpleBolt):

def process_tuple(self, tup):
    freqset, count = tup.values
    log.debug("%s: %d", freqset, count)
    write_result(freqset, count)

if name == 'main': logging.basicConfig( level=logging.DEBUG, filename='/tmp/results.log', format="%(message)s", filemode='a', )

LogResultsBolt().run()

`