· 6 years ago · May 07, 2019, 08:10 AM
1from cassandra.cluster import Cluster
2from cassandra.query import dict_factory
3def create_keyspace(session, keyspace):
4session.execute("""
5CREATE KEYSPACE IF NOT EXISTS """+keyspace+"""
6WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '1' }
7""")
8def create_table(session, keyspace, table):
9session.execute("""
10CREATE TABLE IF NOT EXISTS """+ keyspace+"""."""+table+""" (
11user_id int ,
12avg_movie_rating float,
13PRIMARY KEY(user_id)
14)
15""")
16def push_data_table(session, keyspace, table, userId, avgMovieRating):
17session.execute(
18"""
19INSERT INTO """+keyspace+"""."""+table+""" (user_id, avg_movie_rating)
20VALUES (%(user_id)s, %(avg_movie_rating)s)
21""",
22{
23'user_id': userId,
24'avg_movie_rating': avgMovieRating
25}
26)
27def get_data_table(session, keyspace, table):
28rows = session.execute("SELECT * FROM "+keyspace+"."+table+";")
29for row in rows:
30print(row)
31def clear_table(session, keyspace, table):
32session.execute("TRUNCATE "+keyspace+"."+table+";")