简体   繁体   中英

Python: MQTT broker messages bulk insert into mysql database

I have subscribed to multiple topics using paho mqtt client. On receiving the messages from broker, I want to store the messages into mysql database. I want to gather the messages altogether before inserting into DB.I have set the threshold say 1000 messages. Only when the threshold is reached the messages has to be inserted into DB altogether at once. I am checking the row_count after cursor.execute(). But it shows the count as 1. So the bulk insert is not happening. here is my sample code snippet

//main.py

#mysql database class
db = MySQLDBClass()

#mqtt client class where subscription,connection to broker,some callbacks   
mqttclient = MyMQTTClient() 
mqttclient.on_message = db.onMessage
mqttclient.loop_forever()

//MySQLDBClass.py

 def __init__(self):

        self.insertcounter = 0
        self.insertStatement = ''
        self.bulkpayload = ''
        self.maxInsert = 1000

    def onMessage(self, client, userdata, msg):

        if  msg.topic.startswith("topic1/"):
            self.bulkpayload += "(" + msg.payload.decode("utf-8") + "," + datetime + "),"
        elif msg.topic.startswith("topic2/"):
            self.insertStatement += "INSERT INTO mydatabase.table1 VALUES (" + msg.payload.decode("utf-8") + "," + datetime + ");"
        elif msg.topic.startswith("topic3/")   
            self.insertStatement += "INSERT INTO mydatabase.table2 VALUES (" +msg.payload.decode("utf-8") + "," + datetime + ");"
        elif msg.topic.startswith("messages"):
            self.insertStatement += "INSERT INTO mydatabase.table3 VALUES ('" + msg.topic + "',"  + msg.payload.decode("utf-8") + "," + datetime + ");"
        else:
            return  # do not store in DB

        self.insertcounter += 1 

        if ( self.insertcounter > self.maxInsert ): 
            if ( self.bulkpayload != '' ):
                self.insertStatement += "INSERT INTO mydatabase.table4 VALUES" + self.bulkpayload + ";"    
                self.bulkpayload = ''

            cursor.execute(self.insertStatement)
            cursor.commit()
            print (cursor.rowcount) #prints always count as one , expecting bulk count 
            self.insertcounter  = 0
            self.insertStatement = ''

with pymysql module, execute can only execute only one query at a time whereas by using mysql-connector-python, we can set multi=True in execute(mutli=true) to execute multiple statements. https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM