Python Data Persistence – User-defined Types
While executing the queries, Python data types are implicitly parsed to corresponding CQL types as per the following table:(figure 12.1)
Python Type | CQL Type |
None | NULL |
Bool | Boolean |
Float | Float, double |
Int, long | Int, bigint , variant , smallest, tinyint , counter |
Decimal, decimal | Decimal |
Str , Unicode | Ascii, varchar , test |
Buffer, byte array | Blob |
Date | Date |
Datetime | Timestamp |
Time | Time |
List , tuple
Generator |
List |
Set , frozenest | Set |
Dict , OrderedDist | Map |
Uuid.UUID | Timeuuid.uuid |
- Data Persistence – Python – PyMongo
- Python Data Persistence – RDBMS Concepts
- Program to Handle Precision Values in Python
In addition to the above built-in CQL data types, the Cassandra table may have a column of a user-defined type to which an object of Python class can be mapped.
Cassandra provides a CREATE TYPE statement to define a new user-defined type which is used as a type for a column in a table defined with the CREATE TABLE statement.
In the script given below (Cassandra-udt.py), we define a Cassandra user-defined type named ‘contacts’ and use it as the data type of the ‘contact’ column in the ‘users’ table. The register__user_type ( ) method of cluster object helps us to map Python class ‘Contactlnfo’ to the user-defined type.
Example
#cassandra-udt.py from cassandra.cluster import Cluster cluster = Cluster(protocol_version=3) session = cluster.connect( ) session.set_keyspace(1mykeyspace1) session.execute("CREATE TYPE contact (email text, phone text)") session.execute("CREATE TABLE users (userid int PRIMARY KEY, name text, contact frozen<contact>)") class Contactlnfo: def__init__(self, email, phone): self.email = email self.phone = phone cluster.register_user_type('mykeyspace', 'contact', Contactlnfo) # insert a row using an instance of Contctlnfo session.execute("INSERT INTO users (userid, name, contact) VALUES (%s, %s, %s)", (1, .'Admin', Contactlnfo ("admin@ testserver.com", '9988776655')))
The following display of the CQL shell confirms the insertion operation of the above script.
cq1sh:mykeyspace> select * from users; userid | contact | name ------------+------------------------------------------------------------+----------- 1 | {email: 'admin@testserver.com', phone: '9988776655'} | Admin (1 rows)
In this chapter, we learned about the basic features of the Cassandra database, and importantly how to perform read/write operations on it with Python.
Final thoughts
This is also the last chapter of this book. Starting with the basics of Python, we learned how to work with different data storage formats. This work is neither a Python handbook nor a SQL/NoSQL database guide. Instead, it is intended to be a simple and practical explanation of Python interfaces with different data persistence avenues available today.
This book concentrates primarily on basic CRUD operations on SQL and NoSQL databases and other data file formats. However, these techniques are merely complementary to the real data crunching a data scientist needs to perform. The next logical thing for a curious reader would be to acquire data manipulation and visualization skills for which Python has very rich and powerful libraries.
1 hope you enjoyed this book as much as 1 did bring to you. Feedback, suggestions, and corrections if any are most welcome so that subsequent editions can be improved.
Thanks.