/
migrate_data_mongo.py
215 lines (187 loc) · 6.43 KB
/
migrate_data_mongo.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/python
import pymongo
from pymongo import MongoClient
from bson.objectid import ObjectId
from optparse import OptionParser
from pymongo import ReadPreference
try:
from collections import OrderedDict
except ImportError:
# python 2.6 or earlier, use backport
from ordereddict import OrderedDict
import sys
import time
import datetime
def get_cli_options():
dt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
parser = OptionParser(usage="usage: python %prog [options]",
description=""" MONGODB DATA MIGRATE""")
parser.add_option("--H1", "--from_host",
dest="s_host",
default="localhost",
metavar="host:port"
)
parser.add_option("--H2", "--to_host",
dest="d_host",
default="localhost",
metavar="host:port"
)
parser.add_option("--C1", "--from_coll",
dest="s_coll",
default="test",
metavar="source_collection"
)
parser.add_option("--C2", "--to_coll",
dest="d_coll",
default="test",
metavar="dest_collection"
)
parser.add_option("--D1", "--from_db",
dest="s_db",
default="test",
metavar="source_database"
)
parser.add_option("--D2", "--to_db",
dest="d_db",
default="test",
metavar="dest_database"
)
parser.add_option("--T1", "--s_date",
dest="s_date",
default="2015-04-30 00:00:00",
metavar="start_date"
)
parser.add_option("--T2", "--e_date",
dest="e_date",
default=dt,
metavar="end_date"
)
parser.add_option("--I2", "--col",
dest="col_nm",
default="mtime",
metavar="mtime"
)
parser.add_option("--I3", "--is_inr",
dest="is_inr",
default="N",
metavar="is_inr"
)
(options, args) = parser.parse_args()
return options
def objectid (day):
start_date = str(day)
date_1 = datetime.datetime.strptime(start_date,"%Y-%m-%d %H:%M:%S")
end_date = date_1 + datetime.timedelta(hours=1)
s=datetime.datetime(date_1.year,date_1.month,date_1.day,date_1.hour)
d=datetime.datetime(end_date.year,end_date.month,end_date.day,end_date.hour)
objmin =ObjectId.from_datetime(s)
objmax =ObjectId.from_datetime(d)
#print str(date_1),str(end_date),objmin,objmax
return objmin,objmax,end_date
def from_client():
options = get_cli_options()
client = MongoClient(options.s_host,document_class=OrderedDict)
client['admin'].authenticate('admin','admin')
db=client[options.s_db]
collection=db[options.s_coll]
return collection
def to_client():
options = get_cli_options()
client = MongoClient(options.d_host)
client['admin'].authenticate('admin','admin')
db=client[options.d_db]
collection=db[options.d_coll]
return collection
def timestamp_from_objectid(objectid):
result = 0
try:
timeStamp = time.mktime(objectid.generation_time.timetuple())
timeArray = time.localtime(timeStamp)
createtime=time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
except:
pass
print createtime
def mrigate_date():
options = get_cli_options()
stday=options.s_date
enday=options.e_date
return str(stday),str(enday)
def inr_mrigate_data():
count=0
coll_s=from_client()
coll_d=to_client()
options = get_cli_options()
var_date = datetime.datetime.strptime(options.e_date,"%Y-%m-%d %H:%M:%S")
end_date = (var_date + datetime.timedelta(hours=-8))
v_end_dt=datetime.datetime(end_date.year,end_date.month,end_date.day,end_date.hour,end_date.minute,end_date.second)
iter = coll_s.find({options.col_nm:{"$gte":v_end_dt}})
try:
for doc in iter:
dt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
count=count+1
userObjectId=coll_d.replace_one({"_id":doc['_id']},doc,upsert=True)
if count%20000==0:
print "%d increment commited" %(count),dt
else:
pass
except Exception as e:
print e
print "exit..."
print
sys.exit()
print "sum %d increment commited" %(count)
def mrigate_data():
batch=10000
sum=0
count=0
coll_s=from_client()
coll_d=to_client()
stday,endate=mrigate_date()
while str(stday)<=str(endate):
operations = []
objmin,objmax,endat=objectid(stday)
stday=endat
iter = coll_s.find({"_id":{"$gte":objmin, "$lt":objmax}})
try:
doc=OrderedDict()
for doc in iter:
operations.append(doc)
if (len(operations)==batch):
coll_d.insert_many(operations,ordered=False)
operations = []
count=count+1
if count%10==0:
dt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
print "%d commited" %(count*batch),dt
else:
pass
if (len(operations)> 0):
coll_d.insert_many(operations,ordered=False)
sum=sum+len(operations)
else:
pass
except KeyboardInterrupt:
print "exit .."
print
print "%d commit" %(count),dt
print
sys.exit()
if str(stday)>str(endate):
dt=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
print "%s %d commited " %(endat,count*batch+len(operations)+sum)
else:
pass
def main():
options = get_cli_options()
is_inr=options.is_inr
if is_inr=='N' or is_inr=='n':
mrigate_data()
elif is_inr=="y":
inr_mrigate_data()
elif is_inr=="Y":
mrigate_data()
inr_mrigate_data()
else:
pass
if __name__ == '__main__':
main()