-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cassandra.py
109 lines (95 loc) · 4.3 KB
/
Cassandra.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
from cassandra.auth import PlainTextAuthProvider
from cassandra.cluster import Cluster
from config import database_config
from Logger import Logging
logger_obj = Logging('Advance Image Downloader') # Creating a custom based logger
logger_obj.initialize_logger() # Instantiating the logger object
class Cassandra:
def __init__(self):
"""
This function will instantiate the session for the Cassandra database
"""
try:
cloud_config = {
'secure_connect_bundle': database_config.cloud_config_path
}
auth_provider = PlainTextAuthProvider(database_config.cassandra_uname,
database_config.cassandra_password)
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
self.session = cluster.connect()
except Exception as e:
logger_obj.print_log('(Cassandra.py(__init__) - Something went wrong ' + str(e), 'exception')
raise Exception(e)
def connect_keyspace(self):
"""
This function will use the given keyspace as the default method to work on.
"""
try:
self.session.set_keyspace(database_config.keyspace_name)
except Exception as e:
logger_obj.print_log('(Cassandra.py(connect_keyspace) - Something went wrong ' + str(e), 'exception')
raise Exception(e)
def create_table(self):
"""
This function will create the table if it does not exists in the keyspace
"""
try:
self.session.execute('CREATE TABLE IF NOT EXISTS {} '
'(id UUID, email text, url text, PRIMARY KEY (id, email, url));'
.format(database_config.table_name))
except Exception as e:
logger_obj.print_log('(Cassandra.py(create_table) - Something went wrong ' + str(e), 'exception')
raise Exception(e)
def select_query(self, req_id):
"""
This function will execute and return the select query on the table
:param req_id: Unique request id for the request generated by the user
:return: Results after the selection query
"""
try:
return self.session.execute('SELECT id, email, url FROM {} WHERE id={}'.format(database_config.table_name,
req_id))
except Exception as e:
logger_obj.print_log('(Cassandra.py(select_query) - Something went wrong ' + str(e), 'exception')
raise Exception(e)
def insert_url(self, uuid, email, url):
"""
This function will insert the data into the table
:param uuid: It is a unique user id object
:param email: email of the user
:param url: url of the search query
"""
try:
self.session.execute(
"INSERT INTO " + database_config.table_name + " (id, email, url) VALUES (%s, %s, %s)",
(uuid, email, url))
except Exception as e:
logger_obj.print_log('(Cassandra.py(insert_url) - Something went wrong ' + str(e), 'exception')
raise Exception(e)
def shutdown(self):
"""
This function will close the cassandra session
"""
try:
self.session.shutdown()
except Exception as e:
logger_obj.print_log('(Cassandra.py(shutdown) - Something went wrong ' + str(e), 'exception')
raise Exception(e)
def delete_url(self, req_id):
"""
This function will delete the url for given request ID
"""
try:
self.session.execute('DELETE FROM {} WHERE id={}'.format(database_config.table_name, req_id))
except Exception as e:
logger_obj.print_log('(Cassandra.py(shutdown) - Something went wrong ' + str(e), 'exception')
raise Exception(e)
def drop_table(self):
"""
This function will drop the given table from the keyspace
"""
try:
self.session.execute('DROP TABLE IF EXISTS {}'.format(database_config.table_name))
except Exception as e:
logger_obj.print_log('(Cassandra.py(drop_table) - Something went wrong ' + str(e), 'exception')
raise Exception(e)