-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert.py
188 lines (165 loc) · 6.17 KB
/
insert.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import csv
import psycopg2
import os
import pandas as pd
import time
table_master = [[0,0] for i in range(3)] # data which will be written in master table is written here
table_slave_niranjan = [[0,0] for i in range(3)] # data which will be written in Niranjan's table is written here
table_slave_sampat = [[0,0] for i in range(3)] # data which will be written in Sampat's Desktop table is written here
table_slave_sampat_l = [[0,0] for i in range(3)] # data which will be written in Sampat's Laptop table is written here
start_time = time.time() #this is used to calculate elapsed time for insertion data into the servers
print("Connecting to all the servers")
pid1 = 2347 #port number
pid2 = 5434 #port number
sD = []
sL = []
nD = []
cur_sD = []
cur_sL = []
cur_nD = []
#establishing connection with all the servers in Sampat's Desktop
for i in range (3):
sD.append(psycopg2.connect(host="Enter IP Here",port=pid1, database="postgres", user="postgres", password="it701"))
pid1 -= 1
#establishing connection with all the servers in Sampat's Laptop
for i in range (3):
nD.append(psycopg2.connect(host="Enter IP Here",port=pid2, database="postgres", user="postgres", password="it701"))
pid2 -= 1
pid2 += 3
#establishing connection with all the servers in Niranjan's Desktop
for i in range (3):
sL.append(psycopg2.connect(host="Enter IP Here",port=pid2, database="postgres", user="postgres", password="it701"))
pid2 -= 1
print("All servers are connected")
#assigning cursors
for i in range (3):
cur_sD.append(sD[i].cursor())
for i in range (3):
cur_sL.append(sL[i].cursor())
for i in range (3):
cur_nD.append(nD[i].cursor())
#cleaning tables
for i in range (3):
cur_sD[i].execute("TRUNCATE TABLE dictionary1{};".format(i+1))
for i in range (3):
cur_sL[i].execute("TRUNCATE TABLE dictionary2{};".format(i+1))
for i in range (3):
cur_nD[i].execute("TRUNCATE TABLE dictionary3{};".format(i+1))
print("Partitioning Database...")
#writing data into all the servers
with open('diction.csv','r') as f: #taking data from database found at wordnet.princton.edu
reader = csv.reader(f)
count = 1
# storing rows by sending queries to appropriate servers
for row in reader:
if count <= 19531:
cur_sD[0].execute("INSERT INTO dictionary11 VALUES (%s, %s)",row)
elif count <= 39062:
cur_sD[1].execute("INSERT INTO dictionary12 VALUES (%s, %s)",row)
elif count <= 58593:
cur_sD[2].execute("INSERT INTO dictionary13 VALUES (%s, %s)",row)
elif count <= 78124:
cur_sL[0].execute("INSERT INTO dictionary21 VALUES (%s, %s)",row)
elif count <= 97665:
cur_sL[1].execute("INSERT INTO dictionary22 VALUES (%s, %s)",row)
elif count <= 117186:
cur_sL[2].execute("INSERT INTO dictionary23 VALUES (%s, %s)",row)
elif count <= 136717:
cur_nD[0].execute("INSERT INTO dictionary31 VALUES (%s, %s)",row)
elif count <= 156248:
cur_nD[1].execute("INSERT INTO dictionary32 VALUES (%s, %s)",row)
else:
cur_nD[2].execute("INSERT INTO dictionary33 VALUES (%s, %s)",row)
#storing map values better efficient processing
if count == 1:
table_master[0][0] = str(row[0])
table_slave_sampat[0][0] = str(row[0])
print("Inserting Data in Server 1 in Sampat's Desktop")
elif count == 19531:
table_slave_sampat[0][1] = str(row[0])
print("Done")
elif count == 19532:
table_slave_sampat[1][0] = str(row[0])
print("Inserting Data in Server 2 in Sampat's Desktop")
elif count == 39062:
table_slave_sampat[1][1] = str(row[0])
print("Done")
elif count == 39063:
table_slave_sampat[2][0] = str(row[0])
print("Inserting Data in Server 3 in Sampat's Desktop")
elif count == 58593:
table_master[0][1] = str(row[0])
table_slave_sampat[2][1] = str(row[0])
print("Done")
elif count == 58594:
table_master[1][0] = row[0]
table_slave_sampat_l[0][0] = str(row[0])
print("Inserting Data in Server 1 in Sampat's Laptop")
elif count == 78124:
table_slave_sampat_l[0][1] = str(row[0])
print("Done")
elif count == 78125:
table_slave_sampat_l[1][0] = str(row[0])
print("Inserting Data in Server 2 in Sampat's Laptop")
elif count == 97665:
table_slave_sampat_l[1][1] = str(row[0])
print("Done")
elif count == 97666:
table_slave_sampat_l[2][0] = str(row[0])
print("Inserting Data in Server 3 in Sampat's Laptop")
elif count == 117186:
table_master[1][1] = row[0]
table_slave_sampat_l[2][1] = str(row[0])
print("Done")
elif count == 117187:
table_master[2][0] = row[0]
table_slave_niranjan[0][0] = str(row[0])
print("Inserting Data in Server 1 in Niranjan's Laptop")
elif count == 136717:
table_slave_niranjan[0][1] = str(row[0])
print("Done")
elif count == 136718:
table_slave_niranjan[1][0] = str(row[0])
print("Inserting Data in Server 2 in Niranjan's Laptop")
elif count == 156248:
table_slave_niranjan[1][1] = str(row[0])
print("Done")
elif count == 156249:
table_slave_niranjan[2][0] = str(row[0])
print("Inserting Data in Server 3 in Niranjan's Laptop")
elif count == 175782:
table_master[2][1] = str(row[0])
table_slave_niranjan[2][1] = str(row[0])
print("Done")
count = count + 1
#closing cursors
for i in range (3):
cur_sD[i].close()
for i in range (3):
cur_sL[i].close()
for i in range (3):
cur_nD[i].close()
#commiting the connection
for i in range (3):
sD[i].commit()
for i in range (3):
sL[i].commit()
for i in range (3):
nD[i].commit()
#closing the connections
for i in range (3):
sD[i].close()
for i in range (3):
sL[i].close()
for i in range (3):
nD[i].close()
print("Time taken to fragment data into all the servers = {} seconds".format(time.time()-start_time))
#write map table
df1 = pd.DataFrame(table_master)
df1.to_csv('master_table.csv', header = False, index=False)
df2 = pd.DataFrame(table_slave_sampat)
df2.to_csv('slave_table_sampat_desktop.csv', header = False, index=False)
df3 = pd.DataFrame(table_slave_sampat_l)
df3.to_csv('slave_table_sampat_laptop.csv', header = False, index=False)
df4 = pd.DataFrame(table_slave_niranjan)
df4.to_csv('slave_table_niranjan_desktop.csv', header = False, index=False)