Created
February 17, 2022 07:29
-
-
Save xuanchien/07fbe8bedd3c380474caa3914876ce07 to your computer and use it in GitHub Desktop.
parallel_migrate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import threading | |
import time | |
import sys | |
print(sys.argv) | |
#source info | |
source_url = sys.argv[1] | |
source_table = sys.argv[2] | |
#dest info | |
dest_url = sys.argv[3] | |
dest_table = sys.argv[4] | |
#others | |
total_threads=int(sys.argv[5]); | |
size=int(sys.argv[6]); | |
interval=size/total_threads; | |
start=0; | |
end=start+interval; | |
for i in range(0,total_threads): | |
if(i!=total_threads-1): | |
select_query = '\"\COPY (SELECT * from ' + source_table + ' WHERE id>='+str(start)+' AND id<'+str(end)+") TO STDOUT\""; | |
read_query = "psql \"" + source_url + "\" -c " + select_query | |
write_query = "psql \"" + dest_url + "\" -c \"\COPY " + dest_table +" FROM STDIN\"" | |
os.system(read_query+'|'+write_query + ' &') | |
else: | |
select_query = '\"\COPY (SELECT * from '+ source_table +' WHERE id>='+str(start)+") TO STDOUT\""; | |
read_query = "psql \"" + source_url + "\" -c " + select_query | |
write_query = "psql \"" + dest_url + "\" -c \"\COPY " + dest_table +" FROM STDIN\"" | |
os.system(read_query+'|'+write_query) | |
start=end; | |
end=start+interval; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment