forked from Abby-Mu/yolodetect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cassandraDB.py
72 lines (63 loc) · 2.39 KB
/
cassandraDB.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
from cassandra.cluster import Cluster
from cassandra.policies import RoundRobinPolicy
# connect to the ks
def connect_key_space():
ster = Cluster(contact_points=['127.0.0.1'],
port=9042,
load_balancing_policy=RoundRobinPolicy())
# keyspace name
keyspacename = "mykeyspace3"
session = ster.connect(keyspace=keyspacename)
return session
def exist_table(table_name, keyspacename):
'''the table exists or not'''
ster = Cluster(contact_points=['127.0.0.1'],
port=9042,
load_balancing_policy=RoundRobinPolicy())
session = ster.connect()
print(ster.metadata.keyspaces[keyspacename].tables.keys())
session.shutdown()
if table_name in ster.metadata.keyspaces[keyspacename].tables.keys():
return True
return False
def exist_keyspace(keyspace):
'''the keyspace exists or not'''
ster = Cluster(contact_points=['127.0.0.1'],
port=9042,
load_balancing_policy=RoundRobinPolicy())
session = ster.connect()
print(ster.metadata.keyspaces.keys())
session.shutdown()
if keyspace in ster.metadata.keyspaces.keys():
return True
return False
def keyandspace():
ster = Cluster(contact_points=['127.0.0.1'],
port=9042,
load_balancing_policy=RoundRobinPolicy())
session = ster.connect()
if not exist_keyspace("mykeyspace3"):
session.execute("CREATE KEYSPACE mykeyspace3 WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};")
if not exist_table("mytable3", "mykeyspace3"):
session.execute('create table mykeyspace3.mytable3(inserttime varchar,predictres varchar,filepath varchar primary key);')
session.shutdown()
def insert_data(filepath, crtime, predictres):
'''insert data'''
keyandspace()
session = connect_key_space()
print(session)
sql = 'insert into mytable3(filepath,inserttime,predictres) values(%s, %s, %s)'
session.execute(sql, (filepath, crtime, predictres))
print(sql)
session.shutdown()
def showdata():
# search data
keyandspace()
session = connect_key_space()
sql = 'select * from mytable3'
rs = session.execute(sql)
session.shutdown()
return rs
# test if the data have been inserted
rr = showdata()
print(rr.current_rows)