zoukankan      html  css  js  c++  java
  • hadoop项目实战--ETL--(二)实现自动向mysql中添加数据

    项目开发

    1 创建数据库db_etl,新建两张表user oder。表结构如第一部分图所示。

    2 编写python脚本,实现自动向mysql中插入数据。

    新建python 项目,目录结构如下图

     

    编写代码如下:

    # _*_ coding:UTF-8 _*_
    '''
    Created on 2016年12月1日
    
    @author: duking
    '''
    import MySQLdb
    import random,string
    import time
    import threading
    '''
    数据库连接
    '''
    def ConnMysql():
        #连接数据库
        conn = MySQLdb.connect(host = "192.168.0.154", user = 'root', passwd = '123456', db = 'db_etl', charset = 'utf8')
        cursor = conn.cursor()
        return conn,cursor
    
    '''
    插入user数据
    '''
    def AddUserInfo(username,passwd):
        
        conn,cursor = ConnMysql()
        
        sql = "insert into userinfo(username,passwd) values(%s,%s)"
    
        param = (username,passwd)
         
        cursor.execute(sql,param)
        
        conn.commit()
        cursor.close()
        conn.close()
        
    '''
    插入order数据
    '''
    def AddOderInfo(warename,price):
        
        conn,cursor = ConnMysql()
        
        sql = "insert into oderinfo(warename,price) values(%s,%s)"
    
        param = (warename,price)
         
        cursor.execute(sql,param)
        
        conn.commit()
        cursor.close()
        conn.close()
    
    '''
    随机产生字符串
    '''
    def Random_Str(randomlength):
        a = list(string.ascii_letters)
        random.shuffle(a)
        return ''.join(a[:randomlength])
        
    
    #随机生成订单信息
    def MakeOderInfo(threadname):
        while(True):
            #随机10~100秒生成一条Oder信息
            time.sleep(random.randint(10,100))   
            AddOderInfo(Random_Str(random.randint(6,10)),float(round(random.uniform(10,100),2)))
            print threadname + ':a new OderInfo is Maked    ' + time.ctime(time.time())
    
    #随机生成用户信息
    def MakeUserInfo(threadname):
        while(True):
            time.sleep(random.randint(20,100))
            AddUserInfo(Random_Str(random.randint(6,10)),Random_Str(random.randint(6,10)))
            print threadname + ':a new UserInfo is Maked    ' +time.ctime(time.time())
        
        
    #python 模块的入口:main函数
    if __name__ == '__main__':
        
        #多线程
        thread_1 = threading.Thread(target=MakeOderInfo,args=('thread_1', ))
        thread_2 = threading.Thread(target=MakeUserInfo,args=('thread_2', ))
        
        #启动线程
        thread_1.start()
        thread_2.start()
            
    
            

            

    注意:python调用mysql需要引入MySQLdb模块,改模块的安装请看另外的教程

    最后,将写好的pythonlinux中运行。

    运行后查看数据库就可以看见数据在不断的增长了。

  • 相关阅读:
    jar 包说明
    WebView 下载
    动画
    activity 做出 dialog 效果
    android 数据报表
    拖动 view
    开发收集
    tomcat 5 comcat 6 区别
    android 屏幕判断
    FPS游戏
  • 原文地址:https://www.cnblogs.com/duking1991/p/6121976.html
Copyright © 2011-2022 走看看