V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yurang
V2EX  ›  Python

使用 Python 编写 Storm 程序,无法将结果写入 MySQL

  •  1
     
  •   yurang · 2016-05-10 15:27:28 +08:00 · 2717 次点击
    这是一个创建于 2900 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我使用 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()
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2634 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:57 · PVG 23:57 · LAX 08:57 · JFK 11:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.