-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_using_execute_batch.py
More file actions
171 lines (148 loc) · 7.21 KB
/
Copy pathmain_using_execute_batch.py
File metadata and controls
171 lines (148 loc) · 7.21 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import psycopg2
from psycopg2.extras import execute_batch
import csv
from dotenv import load_dotenv
import os
import time
import logging
from datetime import datetime
# Load environment variables from .env filew
load_dotenv()
# Define your PostgreSQL connection details from environment variables
db_config = {
'host': os.getenv('DB_HOST'),
'port': os.getenv('DB_PORT'),
'dbname': os.getenv('DB_NAME'),
'user': os.getenv('DB_USER'),
'password': os.getenv('DB_PASSWORD')
}
# Path to your CSV file
csv_file_path = './Seat Cover Review DB - 5.29.2024 (Original).csv'
table_name = 'seat_cover_reviews_20240530_3'
# Set up logging
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
log_filename = f"upload_log_{timestamp}.log"
logging.basicConfig(filename=log_filename, level=logging.INFO,
format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
# Log the start of the process
logging.info("Starting CSV upload process")
print("Starting CSV upload process")
# Function to convert blank strings to None (NULL)
def convert_to_null(value):
return None if value == "" else value
# Function to establish a new connection to the database
def create_connection():
return psycopg2.connect(
host=db_config['host'],
port=db_config['port'],
dbname=db_config['dbname'],
user=db_config['user'],
password=db_config['password']
)
# Function to ensure the connection is open
def ensure_connection_open(conn):
if conn.closed != 0:
conn = create_connection()
return conn
# Function to upload CSV to PostgreSQL in batches, starting from a specific row
def upload_csv_to_postgres(csv_file_path, table_name, batch_size=5000, start_row=0, max_retries=3):
conn = create_connection()
cur = conn.cursor()
try:
with open(csv_file_path, 'r', encoding='ISO-8859-1') as f:
reader = csv.reader(f)
header = next(reader) # Skip the header row
escaped_header = [f'"{col}"' for col in header]
rows = []
total_rows = start_row
total_time = 0
# Create an insert query template
query = f"INSERT INTO {table_name} ({', '.join(escaped_header)}) VALUES ({', '.join(['%s'] * len(header))})"
start_skip_time = time.time()
for _ in range(start_row):
next(reader, None)
end_skip_time = time.time()
skip_duration = end_skip_time - start_skip_time
logging.info(f"Skipped {start_row} rows in {skip_duration:.2f} seconds")
print(f"Skipped {start_row} rows in {skip_duration:.2f} seconds")
for i, row in enumerate(reader, start=start_row + 1):
processed_row = [convert_to_null(value) for value in row]
rows.append(tuple(processed_row))
if (i - start_row) % batch_size == 0:
start_time = time.time()
retry_count = 0
while retry_count <= max_retries:
try:
conn = ensure_connection_open(conn)
cur = conn.cursor()
execute_batch(cur, query, rows)
conn.commit()
break
except (psycopg2.OperationalError, psycopg2.InterfaceError) as e:
logging.error(f"Error inserting batch at row {total_rows}: {e}")
print(f"Error inserting batch at row {total_rows}: {e}")
conn.rollback()
retry_count += 1
if retry_count > max_retries:
raise
logging.info(f"Retrying batch at row {total_rows}, attempt {retry_count}")
print(f"Retrying batch at row {total_rows}, attempt {retry_count}")
time.sleep(5) # Wait before retrying
conn = create_connection()
cur = conn.cursor()
except Exception as e:
logging.error(f"Error inserting batch at row {total_rows}: {e}")
print(f"Error inserting batch at row {total_rows}: {e}")
raise
end_time = time.time()
batch_time = end_time - start_time
total_time += batch_time
total_rows += len(rows)
logging.info(f"Inserted {total_rows} rows in {batch_time:.2f} seconds. Total time: {total_time:.2f} seconds")
print(f"Inserted {total_rows} rows in {batch_time:.2f} seconds. Total time: {total_time:.2f} seconds")
rows = []
# Add a delay between batch inserts
time.sleep(2)
# Insert any remaining rows
if rows:
start_time = time.time()
retry_count = 0
while retry_count <= max_retries:
try:
conn = ensure_connection_open(conn)
cur = conn.cursor()
execute_batch(cur, query, rows)
conn.commit()
break
except (psycopg2.OperationalError, psycopg2.InterfaceError) as e:
logging.error(f"Error inserting final batch at row {total_rows}: {e}")
print(f"Error inserting final batch at row {total_rows}: {e}")
conn.rollback()
retry_count += 1
if retry_count > max_retries:
raise
logging.info(f"Retrying final batch at row {total_rows}, attempt {retry_count}")
print(f"Retrying final batch at row {total_rows}, attempt {retry_count}")
time.sleep(5) # Wait before retrying
conn = create_connection()
cur = conn.cursor()
except Exception as e:
logging.error(f"Error inserting final batch at row {total_rows}: {e}")
print(f"Error inserting final batch at row {total_rows}: {e}")
raise
end_time = time.time()
batch_time = end_time - start_time
total_time += batch_time
total_rows += len(rows)
logging.info(f"Inserted {total_rows} rows in {batch_time:.2f} seconds. Total time: {total_time:.2f} seconds")
print(f"Inserted {total_rows} rows in {batch_time:.2f} seconds. Total time: {total_time:.2f} seconds")
except Exception as e:
logging.error(f"Failed to upload CSV to PostgreSQL: {e}")
print(f"Failed to upload CSV to PostgreSQL: {e}")
finally:
cur.close()
conn.close()
# Upload CSV to PostgreSQL in batches
upload_csv_to_postgres(csv_file_path, table_name)
logging.info("CSV upload complete.")
print("CSV upload complete.")