Cassandra: connecting to multiple clusters via cqlengine

This might be simple or very niche but I had to spend a lot of time figuring it out so I just wanted to put it up somewhere.

Say you have 3 cassandra clusters C_X, C_Y and C_Z and you have tables T_X1, T_X2, T_Y1, T_Z1 such that

C_X hosts T_X1 and T_X2
C_Y hosts T_Y1
C_Z hosts T_Z1

Now you can create connections to all of these like so

cluster_x = Cluster(C_X)
session_x = cluster_x.connect()

cluster_y = Cluster(C_Y)
session_y = cluster_y.connect()

cluster_z = Cluster(C_Z)
session_z = cluster_z.connect()

And then you create some named tables ( )

n_t_x1 = NamedTable(T_X1)
n_t_x2 = NamedTable(T_X2)
n_t_y1 = NamedTable(T_Y1)
n_t_z1 = NamedTable(T_Z1)

but how do you tell cassandra driver which table is hosted on which cluster or which session / connection should it use?


from cassandra.cqlengine import connection as cql_connection
cql_connection.register_connection("CONN_X", C_X)
n_t_x1.__connection__ = "CONN_X"
n_t_x2.__connection__ = "CONN_X"

cql_connection.register_connection("CONN_Y", C_Y)
n_t_y1.__connection__ = "CONN_Y"

cql_connection.register_connection("CONN_Z", C_Z)
n_t_z1.__connection__ = "CONN_Z"


now queries to n_t_x1 will be routed to connection with the name “CONN_X” and so on.

PS: it’s kinda hidden the documentation here [driver 3.7.0]


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s