-
Notifications
You must be signed in to change notification settings - Fork 0
/
wtiproj06_simple_cassandra_client.py
74 lines (60 loc) · 2.21 KB
/
wtiproj06_simple_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
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
from cassandra.cluster import Cluster
from cassandra.query import dict_factory
def create_keyspace(session, keyspace):
session.execute("""
CREATE KEYSPACE IF NOT EXISTS """+keyspace+"""
WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '1' }
""")
def create_table(session, keyspace, table):
session.execute(
"""
CREATE TABLE IF NOT EXISTS """+ keyspace+"""."""+table+""" (
user_id int ,
avg_movie_rating float,
PRIMARY KEY(user_id)
)
"""
)
def push_data_table(session, keyspace, table, userId, avgMovieRating):
session.execute(
"""
INSERT INTO """+keyspace+"""."""+table+""" (user_id, avg_movie_rating)
VALUES (%(user_id)s, %(avg_movie_rating)s)
""",
{
'user_id': userId,
'avg_movie_rating': avgMovieRating
}
)
def get_data_table(session, keyspace, table):
rows = session.execute("SELECT * FROM "+keyspace+"."+table+";")
for row in rows:
print(row)
def clear_table(session, keyspace, table):
session.execute("TRUNCATE "+keyspace+"."+table+";")
def delete_table(session, keyspace, table):
session.execute("DROP TABLE "+keyspace+"."+table+";")
if __name__ == "__main__":
keyspace = "user_ratings"
table = "user_avg_rating"
# utworzenia połączenia z klastrem
cluster = Cluster(['127.0.0.1'], port=9042)
session = cluster.connect()
# utworzenie nowego keyspace
create_keyspace(session, keyspace)
# ustawienie używanego keyspace w sesji
session.set_keyspace(keyspace)
# użycie dict_factory pozwala na zwracanie słowników
# znanych z języka Python przy zapytaniach do bazy danych
session.row_factory = dict_factory
# tworzenie tabeli
create_table(session, keyspace, table)
# umieszczanie danych w tabeli
push_data_table(session, keyspace, table, userId=1337, avgMovieRating=4.2)
# pobieranie zawartości tabeli i wyświetlanie danych
get_data_table(session, keyspace, table)
# czyszczenie zawartości tabeli
clear_table(session, keyspace, table)
get_data_table(session, keyspace, table)
# usuwanie tabeli
delete_table(session, keyspace, table)