· 6 years ago · May 14, 2019, 12:26 PM
1from cassandra.cluster import Cluster
2
3from cassandra.query import dict_factory
4
5def create_keyspace(session, keyspace):
6
7session.execute("""
8
9CREATE KEYSPACE IF NOT EXISTS """+keyspace+"""
10
11WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '1' }
12
13""")
14
15def create_table(session, keyspace, table):
16
17session.execute("""
18
19CREATE TABLE IF NOT EXISTS """+ keyspace+"""."""+table+""" ( user_id int ,
20
21avg_movie_rating float,
22
23PRIMARY KEY(user_id)
24
25)
26
27""")
28
29def push_data_table(session, keyspace, table, userId, avgMovieRating):
30
31session.execute(
32
33"""
34
35INSERT INTO """+keyspace+"""."""+table+""" (user_id, avg_movie_rating)
36
37VALUES (%(user_id)s, %(avg_movie_rating)s)
38
39""",
40
41{
42
43'user_id': userId,
44
45'avg_movie_rating': avgMovieRating
46
47}
48
49)
50
51def get_data_table(session, keyspace, table):
52
53rows = session.execute("SELECT * FROM "+keyspace+"."+table+";")
54
55for row in rows:
56
57print(row)
58
59def clear_table(session, keyspace, table):
60
61session.execute("TRUNCATE "+keyspace+"."+table+";")