我的任务是处理一些数据并将其从 CSV 文件迁移到远程 Postgres 实例中。
我决定使用下面的 Python 脚本:
import csv
import uuid
import psycopg2
import psycopg2.extras
import time
#Instant Time at the start of the Script
start = time.time()
psycopg2.extras.register_uuid()
#List of CSV Files that I want to manipulate & migrate.
file_list=["Address.csv"]
conn = psycopg2.connect("host=localhost dbname=address user=postgres password=docker")
cur = conn.cursor()
i = 1
for f in file_list:
f = open(f)
csv_f = csv.reader(f)
next(csv_f)
for row in csv_f:
# Some simple manipulations on each row
#Inserting a uuid4 into the first column
row.pop(0)
row.insert(0,uuid.uuid4())
row.pop(10)
row.insert(10,False)
row.pop(13)
#Tracking the number of rows inserted
print(i)
i = i + 1
#INSERT QUERY
postgres_insert_query = """ INSERT INTO "public"."address"("address_id","address_line_1","locality_area_street","address_name","app_version","channel_type","city","country","created_at","first_name","is_default","landmark","last_name","mobile","pincode","territory","updated_at","user_id") VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
record_to_insert = row
cur.execute(postgres_insert_query,record_to_insert)
f.close()
conn.commit()
conn.close()
print(time.time()-start)
该脚本在本地测试时运行良好且迅速。但是连接到远程数据库服务器会增加很多延迟。
作为一种解决方法,我将处理过的数据迁移到我的本地 postgres 实例中。
然后我生成了一个迁移数据的 .sql 文件并在远程服务器上手动导入了 .sql 文件。
或者,您还可以使用 Python 的多线程功能,启动到远程服务器的多个并发连接,并为每个连接专用一个隔离的批处理,并刷新数据。
这应该会大大加快您的迁移速度。
我个人没有尝试过多线程方法,因为在我的情况下不需要它。但它似乎非常有效。
希望这有帮助! :)
资源:
CSV Manipulation using Python for Beginners.