-
Notifications
You must be signed in to change notification settings - Fork 0
/
cassandra_client.py
47 lines (39 loc) · 1.77 KB
/
cassandra_client.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
from cassandra.auth import PlainTextAuthProvider
from cassandra.cluster import Cluster
from cassandra.cqlengine import management
from cassandra.cqlengine import connection
from models import SongByName, SongsPlayedByUser, SearchPlayListByName, PlayListFollower, UserFollower, SongInPlayList, \
PlaylistPopularityPrefixed, UserDecreasingPopularityPrefix
from settings import CASSANDRA_NODE, CASSANDRA_PORT, CASSANDRA_USERNAME, CASSANDRA_PASSWORD, CASSANDRA_KEY_SPACE, \
CONNECTION_NAME, REPLICATION_FACTOR
class CassandraClient(object):
"""This Python object is used to handle Apache Cassandra Instance
"""
def __init__(self):
self.client_credentials = PlainTextAuthProvider(
username=CASSANDRA_USERNAME,
password=CASSANDRA_PASSWORD
)
self.cluster = Cluster(
contact_points=[CASSANDRA_NODE],
port=CASSANDRA_PORT,
auth_provider=self.client_credentials,
)
self.session = self.cluster.connect()
self.connection = connection.register_connection(CONNECTION_NAME, session=self.session)
management.create_keyspace_simple(
CASSANDRA_KEY_SPACE,
connections=[CONNECTION_NAME],
replication_factor=REPLICATION_FACTOR
)
@staticmethod
def sync_models():
"""This method is used to sync Cassandra models found in models.py"""
management.sync_table(SongByName)
management.sync_table(SongsPlayedByUser)
management.sync_table(SearchPlayListByName)
management.sync_table(PlayListFollower)
management.sync_table(UserFollower)
management.sync_table(SongInPlayList)
management.sync_table(PlaylistPopularityPrefixed)
management.sync_table(UserDecreasingPopularityPrefix)