python-cassandra

  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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime
import time
import logging
import configparser
from cassandra.cluster import Cluster
from cassandra.cqltypes import DateType
log = logging.getLogger()
log.setLevel('INFO')
class SimpleClient:
session = None
prepared_statement_table1 = None
prepared_statement_table2 = None
output_file = None
def connect(self, nodes):
cluster = Cluster(nodes)
metadata = cluster.metadata
self.session = cluster.connect()
log.info('Connected to cluster: ' + metadata.cluster_name)
for host in metadata.all_hosts():
log.info('Datacenter: %s; Host: %s; Rack: %s',
host.datacenter, host.address, host.rack)
def close(self):
self.session.cluster.shutdown()
self.session.shutdown()
log.info('Connection closed.')
def create_schema(self):
self.session.execute("""CREATE KEYSPACE count_calls WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};""")
self.session.execute("""
CREATE TABLE count_calls.calls_table (
num1 text PRIMARY KEY,
calls map<timestamp, text>
);
""")
self.session.execute("""
CREATE TABLE count_calls.calls_table2 (
num1 text,
started timestamp,
num2 text,
PRIMARY KEY (num1, started)
);
""")
log.info('Count_calls keyspace and schema created.')
def count_calls(self, numbers, start_date, end_date, criteria):
print("begin")
prepared_statement = self.session.prepare("SELECT calls FROM count_calls.calls_table WHERE num1 = ?;")
time_begin = time.time()
for number in numbers:
if len(str(number)) < 3:
continue
number_of_calls = 0
result = {}
#query = "SELECT calls FROM count_calls.calls_table WHERE num1 = '"+str(number)+"';"
for line in self.session.execute(prepared_statement, [str(number)]):
for date, num2 in line.calls.items():
if date < start_date or len(str(num2)) < 3:
continue
elif date <= end_date:
number_of_calls += 1
if num2 in result:
result[num2] += 1
else:
result[num2] = 1
print(result.items())
needed = number_of_calls*criteria/100.
for_count = 100./number_of_calls
output_file = open('/home/natalia/Рабочий стол/cassandraT/output.txt', 'w')
for key in result:
if result[key] > needed:
r = 'r: '+str(number) + '\t' + str(key).split("\r\n")[0] + '\t' + str(result[key])\
+ '\t' + str(result[key]*for_count)
output_file.write(r)
print(r)
time_end = time.time()
print ("work time = " + str(time_end - time_begin))
return
"""def count_calls2(self, numbers, start_date, end_date, criteria):
print("begin2")
prepared_statement = self.session.prepare("SELECT started, num2 FROM count_calls.calls_table2 WHERE num1=?;")
for number in numbers:
if len(str(number)) < 3:
continue
number_of_calls = 0
result = {}
#query = "SELECT started, num2 FROM count_calls.calls_table2 WHERE num1 = '"+str(number)+"';"
for line in self.session.execute(prepared_statement, [str(number)]):
if len(str(line.num2)) < 3 or line.started < start_date:
continue
elif line.started <= end_date :
number_of_calls += 1
if line.num2 in result:
result[line.num2] += 1
else:
result[line.num2] = 1
print(result.items())
needed = number_of_calls*criteria/100.
for_count = 100./number_of_calls
output_file = open('/home/natalia/Рабочий стол/cassandraT/output2.txt', 'w')
for key in result:
if result[key] > needed:
r = 'r: '+str(number) + '\t' + str(key).split("\r\n")[0] + '\t' + str(result[key]) + '\t' + str(result[key]*for_count)
print(r)
output_file.write(r)
return
"""
def count_calls2(self, numbers, start_date, end_date, criteria):
print("begin2")
prepared_statement = self.session.prepare("SELECT started, num2 FROM count_calls.calls_table2 WHERE\
num1=? AND started >= ? AND started <= ?;")
time_begin = time.time()
for number in numbers:
if len(str(number)) < 3:
continue
result = {}
#query = "SELECT num2 FROM count_calls.calls_table2 WHERE num1 = '"+str(number)+"' \
#AND started >= '"+str(start_date)+"' AND started <= '"+str(end_date)+"';"
e = self.session.execute(prepared_statement, [str(number), start_date, end_date])
number_of_calls = len(e)
for line in e:
if len(line.num2) > 3:
if line.num2 in result:
result[line.num2] += 1
else:
result[line.num2] = 1
print(result.items())
needed = number_of_calls*criteria/100.
for_count = 100./number_of_calls
output_file = open('/home/natalia/Рабочий стол/cassandraT/output2.txt', 'w')
for key in result:
if result[key] > needed:
r = 'r: '+str(number) + '\t' + str(key).split("\r\n")[0] + '\t' + str(result[key])\
+ '\t' + str(result[key]*for_count)
print(r)
output_file.write(r)
time_end = time.time()
print ("work time = " + str(time_end - time_begin))
return
prepared_insert_calls_table2 = "INSERT INTO count_calls.calls_table2 (num1, started, num2) VALUES (? ,? ,?);"
prepared_insert_calls_table = "INSERT INTO count_calls.calls_table (num1, calls) VALUES (?, ?);"
prepared_update_calls_table = ""
#copy from calls_table to calls_table2
def insert_double(self, num1, started, num2):
query = "INSERT INTO count_calls.calls_table2 (num1, started, num2) VALUES ('"+str(num1) +\
"','"+str(started)+"','"+str(num2)+"');"
self.session.execute(query)
def update(self, num1, started, num2):
query = "UPDATE count_calls.calls_table SET calls = calls + {'"+str(started)+"' : '"+str(num2)+\
"'} WHERE num1 = '"+str(num1)+"';"
self.session.execute(query)
def insert(self, num1, started, num2):
query = "INSERT INTO count_calls.calls_table (num1, calls) VALUES ('"+str(num1)+"',{ '"+\
str(started)+"' : '"+str(num2)+"' } );"
self.session.execute(query)
def insert_from_calls_to_calls2(self):
added_nums = []
for line in self.session.execute("SELECT * FROM count_calls.calls_table2;"):
self.insert_double(line.num2, line.started, line.num1)
if added_nums.__contains__(line.num1):
self.update(line.num1, line.started, line.num2)
else:
self.insert(line.num1, line.started, line.num2)
added_nums.append(line.num1)
if added_nums.__contains__(line.num2):
self.update(line.num2, line.started, line.num1)
else:
self.insert(line.num2, line.started, line.num1)
added_nums += line.num2
def convert_date(str_date):
d = str_date.replace('.',' ').replace(':', ' ').replace('-', ' ').split(' ')
#print("d.len="+ str(len(d)))
f = ''
if len(d) == 5:
f = '%d.%m.%Y %H:%M'
elif len(d) == 6:
f = '%d.%m.%Y %H:%M:%S'
return datetime.strptime(str_date, f)
def format_date_for_import(date):
whole_date = date.split(" ")
d = whole_date[0].split(".")
return d[2]+"-"+d[1]+"-"+d[0]+" "+whole_date[1]
#before COPY count_calls.calls_table2 (num1, started, num2) FROM 'output_csv';
def format_csv_data_for_import(source, output_csv):
#needed: 79147349111,"{'2013-03-02 08:21:11+0400': '79140656269'}"
#needed: 79147349111,2013-03-02 08:21:11+0400,"79140656269"
data = open(source)
op = open(output_csv, 'w')
i = 0
for line in data:
if i < 1000:
i += 1
el = line.split(";")
if len(el) > 2 and len(el[2]) > 2:
date = format_date_for_import(el[1])
#print(date)
#string_to_write = el[0] + ",\"{'" + date + "': '" + el[2].split("\r\n")[0] + "'}\""
op.write(el[0] + ","+date + ",\"" + el[2].split("\r\n")[0] + "\"" + "\n")
op.write(el[2].split("\r\n")[0] + ","+date + ",\"" + el[0] + "\"" + "\n")
else:
break
def main():
conf = configparser.ConfigParser()
conf.read('/home/natalia/Рабочий стол/cassandraT/config.ini')
dc = open(conf['INPUT']['input_date_and_criteria']).read().split("\n")
criteria = float(dc[0])
start_date = convert_date(dc[1])
end_date = convert_date(dc[2])
numbers = open(conf['INPUT']['input_numbers']).read().split("\n")
print(str(criteria) + "\n" + str(start_date) + "\n" + str(end_date))
print(numbers)
#criteria = 10.
#start_date = convert_date('02.03.2013 3:17:10')
#end_date = convert_date('02.03.2013 8:17:20')
#numbers = ['79532196613']
logging.basicConfig()
client = SimpleClient()
client.connect(['127.0.0.1'])
#client.create_schema()
#time.sleep(10)
#format_csv_data_for_import('/home/natalia/Рабочий стол/cassandraT/all_calls.txt','/home/natalia/Рабочий стол/cassandraT/trytemp.csv')
#client.insert_from_calls_to_calls2()
client.count_calls(numbers, start_date, end_date, criteria)
client.count_calls2(numbers, start_date, end_date, criteria)
client.close()
if __name__ == "__main__":
main()