-
Notifications
You must be signed in to change notification settings - Fork 6
/
loadcsv.py
77 lines (56 loc) · 1.85 KB
/
loadcsv.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
from concurrent import futures
import csv
import queue
import sqlactions
MULTI_ROW_INSERT_LIMIT = 1000
WORKERS = 6
def read_csv(csv_file):
with open(csv_file, encoding="utf-8", newline="") as in_file:
reader = csv.reader(in_file, delimiter="|")
next(reader) # Header row
for row in reader:
yield row
def process_row(row, batch, table_name, conn_params):
batch.put(row)
if batch.full():
sqlactions.multi_row_insert(batch, table_name, conn_params)
return batch
def load_csv(csv_file, table_def, conn_params):
# Optional, drops table if it exists before creating
sqlactions.make_table(table_def, conn_params)
batch = queue.Queue(MULTI_ROW_INSERT_LIMIT)
with futures.ThreadPoolExecutor(max_workers=WORKERS) as executor:
todo = []
for row in read_csv(csv_file):
future = executor.submit(
process_row, row, batch, table_def["name"], conn_params
)
todo.append(future)
for future in futures.as_completed(todo):
result = future.result()
# Handle left overs
if not result.empty():
sqlactions.multi_row_insert(result, table_def["name"], conn_params)
if __name__ == "__main__":
table_def = {
"name": "dummy_data",
"columns": {
"id": "INTEGER",
"job": "VARCHAR(100)",
"company": "VARCHAR(100)",
"name": "VARCHAR(100)",
"sex": "CHAR",
"mail": "VARCHAR(100)",
"birthdate": "DATE",
},
}
conn_params = {
"server": "localhost",
"database": "TutorialDB",
"user": "yourUserName",
"tds_version": "7.4",
"password": "yourStrong(!)Password",
"port": 1433,
"driver": "FreeTDS",
}
load_csv("dummy_data.csv", table_def, conn_params)