-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_data_example.py
107 lines (94 loc) · 3.39 KB
/
mysql_data_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#! /usr/bin/env python
import argparse
import logging
import os
import sys
import zipfile
import MySQLdb
import requests
logger = logging.getLogger(__name__)
URL = 'http://seanlahman.com/files/database/lahman2016-sql.zip'
class MLBDataLoader(object):
def __init__(self, dbhost, username='root', password=None, port=3306):
"""
:param dbhost:
:param username:
:param password:
:param port:
:return:
"""
db = MySQLdb.connect(dbhost, user=username, password=password, port=port)
self.cursor = db.cursor()
self.log = logger
def run(self):
"""
:return:
"""
self.log.info('Downloading MLB sql file from URL "%s"', URL)
sql_file = self.download_sql_file(URL)
self.log.info('The MLB sql file is "%s"', sql_file)
self.log.info('Loading SQL to DB ...')
self.load_sql_to_db(sql_file)
self.log.info('Complete: Successfully load SQL data to mysql database')
def download_sql_file(self, url):
"""
:param url:
:return:
"""
zipped = '/tmp/tmp.zip'
unzipped = '/tmp/unzipped'
with open(zipped, "wb") as f:
response = requests.get(url)
f.write(response.content)
zipfile.ZipFile(zipped).extractall(unzipped)
if os.path.exists(zipped):
os.remove(zipped)
for root, _, files in os.walk(unzipped):
for f in files:
if f.endswith('.sql'):
ff = os.path.join(root, f)
self.log.info('Got SQL file %s', ff)
return ff
self.log.error('No SQL file')
return
def load_sql_to_db(self, sql_file):
"""
:param sql_file:
:return:
"""
statement = ''
for line in open(sql_file):
line = line.strip()
if line.startswith('--'): # ignore sql comment lines
continue
if not line.endswith(';'):
statement += line
else: # when you get a line ending in ';' then exec statement and reset for next statement
statement += line
try:
self.log.info('Execute SQL command %s', statement)
self.cursor.execute(statement)
except Exception as exc:
self.log.info(exc)
finally:
statement = ''
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Tool to load SQL data to MySQL database')
parser.add_argument('--dbhost', required=True, help='The database host')
parser.add_argument('--username', default='root', help='The database username')
parser.add_argument('--password', help='The database password')
parser.add_argument('--port', default=3306, type=int, help='The database password')
logging.basicConfig(format='%(asctime)s.%(msecs)03d %(levelname)s %(name)s %(threadName)s: %(message)s',
datefmt='%Y-%m-%dT%H:%M:%S',
level=logging.INFO,
stream=sys.stdout)
args = parser.parse_args()
rc = 0
try:
loader = MLBDataLoader(args.dbhost, username=args.username, password=args.password, port=args.port)
loader.run()
except Exception as exc:
logger.exception(exc)
rc = 1
finally:
sys.exit(rc)