-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathudt.py
More file actions
31 lines (21 loc) · 923 Bytes
/
udt.py
File metadata and controls
31 lines (21 loc) · 923 Bytes
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
from cassandra.cluster import Cluster
cluster = Cluster()
session = cluster.connect()
session.set_keyspace('university')
session.execute("CREATE TYPE address (street text, zipcode int)")
session.execute("CREATE TABLE user (id int PRIMARY KEY, location frozen<address>)")
# create a class to map to the "address" UDT
class Address(object):
def __init__(self, street, zipcode):
self.street = street
self.zipcode = zipcode
#cluster.register_user_type('university', 'address', Address)
data = [Address("123 Main St.", 78723), Address("123 Main St.", 78723)]
# insert a row using an instance of Address
for idx,d in ennumerate(data):
session.execute("INSERT INTO user (id, location) VALUES (%s, %s)",
(idx, d))
# results will include Address instances
results = session.execute("SELECT * FROM user")
row = results.one()
print (row.id, row.location.street, row.location.zipcode)