-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcassandra_upsert.py
60 lines (44 loc) · 1.39 KB
/
cassandra_upsert.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
import random
from cassandra.cluster import Cluster
import time
import uuid
cluster = Cluster(['127.0.0.1'])
session = cluster.connect()
session.execute("""
CREATE KEYSPACE IF NOT EXISTS test
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}
""")
session.execute("""
CREATE TABLE IF NOT EXISTS test.test_table (
id UUID PRIMARY KEY,
channel_id INT,
data TEXT
)
""")
row_id = uuid.uuid4()
# Insert initial row
def insert_row():
session.execute(
"INSERT INTO test.test_table (id, channel_id, data) VALUES (%s, %s, %s)",
(row_id, random.randint(1, 100000), 'initial_value')
)
def edit_row():
session.execute("UPDATE test.test_table SET data = %s WHERE id = %s", ('updated_value', row_id))
# session.execute("UPDATE test.test_table SET data = %s WHERE id = %s IF EXISTS", ('updated_value', row_id))
time.sleep(0.01)
def delete_row():
session.execute("DELETE FROM test.test_table WHERE id = %s", (row_id,))
time.sleep(0.01)
insert_row()
delete_row()
edit_row()
final_state = session.execute("SELECT * FROM test.test_table WHERE id = %s", (row_id,)).one()
print(final_state)
row_id = uuid.uuid4()
insert_row()
edit_row()
final_state = session.execute("SELECT * FROM test.test_table WHERE id = %s", (row_id,)).one()
print(final_state)
session.execute("DROP TABLE test.test_table")
session.execute("DROP KEYSPACE test")
cluster.shutdown()