-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnssync.py
308 lines (281 loc) · 9.96 KB
/
nssync.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import os;
import simplejson as json;
import uuid;
import time;
from datetime import datetime;
from datetime import date;
from elasticsearch import Elasticsearch;
from elasticsearch.exceptions import NotFoundError;
from cassandra.cluster import Cluster;
def datetimeFromIsoFormat(strIsoFormat):
year = int(strIsoFormat[0:4]);
month = int(strIsoFormat[5:7]);
day = int(strIsoFormat[8:10]);
hour = int(strIsoFormat[11:13]);
minute = int(strIsoFormat[14:16]);
second = int(strIsoFormat[17:19]);
microsecond = int(strIsoFormat[20:]);
return datetime(year,month,day,hour,minute,second,microsecond);
class NsSynchronizer:
defaultTimestampField = '';
count = 0;
timeline = datetime(1900,1,1,0,0,0);
es = Elasticsearch([{"host": "localhost", "port": 9200}]);
cluster = Cluster(contact_points="127.0.0.1",port=9042);
encoder = json.JSONEncoder();
delay = 10;
# Constructor
def __init__(self,timestampField = 'timestamp'):
self.defaultTimestampField = timestampField;
print "Synchronizer Created";
# Runs continuously always synchronizing
def keepsynchronizing(self):
while (1 == 1):
self.sync();
time.sleep(self.delay)
self.timeline = datetime.now();
self.timeline = datetime(self.timeline.year,self.timeline.month,self.timeline.day,self.timeline.hour,self.timeline.minute,self.timeline.second);
# Synchronize ElasticSearch and Cassandra
def sync(self):
print "Synchronizer Started";
self.dothesync();
print "Synchronizer Finished";
# TODO Dummy - Remove
def dothesync2(self):
searchResultES = self.fetchES();
print json.dumps(searchResultES, sort_keys = False, indent = 4)
searchResultCS = self.fetchCS();
for user in searchResultCS:
print user.id,user.username
# TODO Dummy - Remove
def fetchES(self):
es = elf.es;
# Define the timestamp field.
timestampField = self.defaultTimestampField;
args = locals();
baseDate = datetime(1900,1,1,0,0,0);
searchBodyES = '''
{
"query": {
"range": {
"%s": {
"gte": "%s"
}
}
}
}
''' % (timestampField,self.timeline.isoformat());
searchResultES = es.search(body = searchBodyES);
return searchResultES;
# Index an object in ElasticSearch from a Cassandra Row
def ESindexCSRow(self,indexName,type,row):
rowdic = {};
for (field,value) in row.__dict__.items():
if field == 'id':
continue;
else:
if isinstance(value,datetime):
rowdic[field] = value.isoformat();
else:
rowdic[field] = value;
print "\t\t\t\t\t\t%s => %s" % (field,value);
jsonObject = self.encoder.encode(rowdic);
self.es.index(indexName, type, jsonObject, id=row.id, params=None)
# Update Cassandra from an ElasticSearch Object
# Unfortunatelly, due to Cassandra search restrictions on non-key values,
# it's needed to perform a DELETE and and INSERT instead of an UPDATE
def CSupdatexESobject(self,session,indexName,type,row,objInES):
source = objInES['_source'];
newrow = {u'id': objInES['_id']};
for field in source:
value = source[field];
newrow[field] = value;
print '\t\t\t\t\t\t\t',newrow;
query = "DELETE FROM %s.%s WHERE id = %s" % (indexName,type,objInES['_id']);
session.execute(query);
query = "INSERT INTO %s.%s (%s) VALUES (%s)";
columnsPart = "";
valuesPart = "";
valuesList = [];
for field in newrow:
value = newrow[field];
if columnsPart != "":
columnsPart = columnsPart + ","
valuesPart = valuesPart + ","
columnsPart = columnsPart + field;
if field == 'id':
valuesPart = valuesPart + "%s";
elif isinstance(value,(int,long)):
valuesPart = valuesPart + "%d";
elif isinstance(value,(float)):
valuesPart = valuesPart + "%f";
elif isinstance(value,(datetime,date)):
valuesPart = valuesPart + "'%s'";
elif isinstance(value,(str,unicode)):
valuesPart = valuesPart + "'%s'";
else:
valuesPart = valuesPart + "%s";
valuesList.append(value);
query = query % (indexName,type,columnsPart,valuesPart);
query = query % tuple(valuesList);
session.execute(query);
# Update Cassandra from an ElasticSearch Object
# Unfortunatelly, due to Cassandra search restrictions on non-key values,
# it's needed to perform a DELETE and and INSERT instead of an UPDATE
def CSinsertESobject(self,session,indexName,type,objInES):
source = objInES['_source'];
newrow = {u'id': objInES['_id']};
for field in source:
value = source[field];
newrow[field] = value;
print '\t\t\t\t\t\t\t',newrow;
query = "INSERT INTO %s.%s (%s) VALUES (%s)";
columnsPart = "";
valuesPart = "";
valuesList = [];
for field in newrow:
value = newrow[field];
if columnsPart != "":
columnsPart = columnsPart + ","
valuesPart = valuesPart + ","
columnsPart = columnsPart + field;
if field == 'id':
valuesPart = valuesPart + "%s";
elif isinstance(value,(int,long)):
valuesPart = valuesPart + "%d";
elif isinstance(value,(float)):
valuesPart = valuesPart + "%f";
elif isinstance(value,(datetime,date)):
#value = datetime(value.year,value.month,value.day,value.hour,value.minute,value.second);
valuesPart = valuesPart + "'%s'";
elif isinstance(value,(str,unicode)):
valuesPart = valuesPart + "'%s'";
else:
valuesPart = valuesPart + "%s";
valuesList.append(value);
query = query % (indexName,type,columnsPart,valuesPart);
query = query % tuple(valuesList);
session.execute(query);
# TODO This method doesn't work, once there are cassandra limitations on updating primary keys
def CSupdateESobject(self,session,indexName,type,objInES):
source = objInES['_source'];
fieldsStr = "";
valuesStr = "";
valuesList = [indexName,type];
dic = {};
for field in source:
value = source[field];
if (field == "id"):
continue;
dic[field] = value;
if fieldsStr == "":
if isinstance(value, (int, long)):
fieldsStr = "%s = %s" % (field, '%d');
elif isinstance(value, (float)):
fieldsStr = "%s = %s" % (field, '%f');
else:
fieldsStr = "%s = '%s'" % (field, '%s');
else:
if isinstance(value, (int, long)):
fieldsStr = fieldsStr + ", %s = %s" % (field, '%d');
elif isinstance(value, (float)):
fieldsStr = fieldsStr + ", %s = %s" % (field, '%f');
else:
fieldsStr = fieldsStr + ", %s = '%s'" % (field, '%s');
valuesList.append(value);
valuesList.append(objInES['_id']);
updateStr = "UPDATE %s.%s SET ";
whereStr = " WHERE id = '%s'";
clause = updateStr + fieldsStr + whereStr;
tupleobj = tuple(valuesList);
query = (clause % tupleobj);
print query;
# Synchronize a type
def syncType(self,session,indexName,type):
# Search for information on Cassandra and compare with those in ElasticSearch
print "\t\t\tSynchronizing type %s" % (type)
print "\t\t\tLooping over Cassandra data"
timestampField = self.defaultTimestampField;
baseDateTime = self.timeline;
query = "SELECT * FROM %s WHERE %s > '%s' ALLOW FILTERING" % (type,timestampField,baseDateTime.isoformat());
#print "==> ",query;
rows = session.execute(query);
for row in rows:
print "\t\t\t\t",row;
try:
objInES = self.es.get(indexName,row.id,type)
print "\t\t\t\t\t",objInES;
timestampInES = objInES['_source']['timestamp'];
timestampInCS = row.timestamp.isoformat();
if timestampInES > timestampInCS:
print "\t\t\t\t\t\tElasticSearch has newer information. Updating Cassandra..."
self.CSupdatexESobject(session,indexName,type,row,objInES);
elif timestampInES < timestampInCS:
print "\t\t\t\t\t\tCassandra has newer information. Updating ElasticSearch..."
self.ESindexCSRow(indexName,type,row);
else:
print "\t\t\t\t\t\tInformation has same timestamp. Skipping..."
except NotFoundError:
print "\t\t\t\t\tNot in ElasticSearch. Inserting..."
self.ESindexCSRow(indexName,type,row);
# TODO Search for information on ElasticSearch that isn't on Cassandra. Insert on Cassandra
print "\t\t\tLooping over ElasticSearch data"
# Define the timestamp field.
timestampField = self.defaultTimestampField;
searchBodyES = '''
{
"query": {
"range": {
"%s": {
"gte": "%s"
}
}
}
}
''' % (timestampField,self.timeline.isoformat());
searchResultES = self.es.search(indexName, type, searchBodyES);
hits = searchResultES['hits']['hits'];
#print json.dumps(hits, sort_keys = False, indent = 4);
for hit in hits:
query = "SELECT * FROM %s.%s WHERE id = %s" % (indexName,type,hit['_id']);
rows = session.execute(query);
size = rows.__len__();
if size == 0:
self.CSinsertESobject(session,indexName,type,hit)
# TODO What to do with new indexes/types?
# Synchronize an index
def syncIndex(self,systemSession,indexName):
print "\tSynchronizing Index %s" % (indexName)
cluster = self.cluster;
print "\t\tFetching types";
query = "SELECT * FROM SCHEMA_COLUMNFAMILIES WHERE KEYSPACE_NAME = '%s'" % (indexName)
types = systemSession.execute(query);
for type in types:
session = cluster.connect(indexName);
self.syncType(session,indexName,type.columnfamily_name);
# Actually Perform the Synchronization
def dothesync(self):
cluster = self.cluster;
systemSession = cluster.connect('system');
print "Fetching indexes to Synchronize"
query = "SELECT * FROM SCHEMA_KEYSPACES";
keyspaces = systemSession.execute(query);
for keyspace in keyspaces:
if (keyspace.keyspace_name in ('system','system_traces')):
continue;
print "Synchronizing", keyspace.keyspace_name;
self.syncIndex(systemSession,keyspace.keyspace_name);
return;
# TODO Dummy Remove
def test(self):
es = Elasticsearch(); # TODO: Use specified server
# datetimes will be serialized
es.index(index="my-index", doc_type="test-type", id=42, body={"any": "data", "timestamp": datetime.now()})
#{u'_id': u'42', u'_index': u'my-index', u'_type': u'test-type', u'_version': 1, u'ok': True}
# but not deserialized
result = es.get(index="my-index", doc_type="test-type", id=42)['_source']
#{u'any': u'data', u'timestamp': u'2013-05-12T19:45:31.804229'}
print(result)
s = NsSynchronizer();
#s.sync();
s.keepsynchronizing();