hadoop項目實戰--ETL--(二)實現自動向mysql中添加數據

項目開發

1 建立數據庫db_etl,新建兩張表user oder。表結構如第一部分圖所示。python

2 編寫python腳本,實現自動向mysql中插入數據。mysql

新建python 項目,目錄結構以下圖linux

 

 

編寫代碼以下:sql

# _*_ 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中運行。多線程

運行後查看數據庫就能夠看見數據在不斷的增加了。dom

相關文章
相關標籤/搜索