A regular re-run written by Python to obtain database data.
Kids shoes with big data often write scheduled task run data, because dependencies between tasks (generally downstream dependent on upstream data output) often lead to data acquisition failure, because many people find that data fails
You can view the logs and then manually execute your tasks. Next, I implement an automatic repeated database fetch function. If it fails, it will be automatically retrieved again until the data is obtained.
Create a data table:
CREATE TABLE `testtable` ( 2 `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 3 `name` varchar(20) NOT NULL, 4 PRIMARY KEY (`id`) 5 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
At the beginning, the data table is empty. When the script retries for 3rd seconds, it inserts data like a database.
The following is the python code implementation:
#!/usr/bin/env python #-*- coning:utf-8 -*- 3 4 import MySQLdb 5 from time import sleep 6 7 class GetData(object): 8 def __init__(self): 9 self.conn = '' 10 self.host = '127.0.0.1' 11 self.port = 3306 12 self.user = 'root' 13 self.passwd = '123456' 14 self.db = 'test' 15 self.cnum = 5 #set retry number 16 17 def init_connect(self): 18 self.conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.passwd, db=self.db, port=self.port, 19 charset='utf8') 20 21 def get_data(self): 22 self.init_connect 23 cur = self.conn.cursor 24 sql = "select * from testtable" 25 cur.execute(sql) 26 rs = cur.fetchall 27 cur.close 28 self.conn.close 29 return rs 30 31 def run(self): 32 count = 1 33 while (count <= self.cnum): 34 rs = self.get_data 35 if len(rs) > 0: 36 print len(rs) 37 break 38 39 print count 40 sleep(10) 41 count += 1 42 43 if __name__ == '__main__': 44 gd = GetData 45 gd.run
You can manually execute the following SQL statement when the code is executed to 3rd seconds.
insert into testtable(`name`) values ('123'),('456'),('789'),('1111'),('3222'),('444');
The following is a script for a scheduled task.
00 08 * * * cd /home/python/lsh_sync; python getdata.py >> getdata.log 2>&1
The above is a regular re-run task written in Python to obtain database data. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!