我使用 Python 作为编程语言写一个关联关系挖掘的程序部署在 Storm 集群,将 jar 包提交到集群后无法将结果写入数据库,也无法生成 log 文件,贴上写入结果的 bolt 代码,希望大家帮忙看看,不胜感激:
#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)#写入 log 中
write_result(freqset, count)
if __name__ == '__main__':
logging.basicConfig(
level=logging.DEBUG,
filename='/tmp/results.log',
format="%(message)s",
filemode='a',
)
LogResultsBolt().run()