How to connect to Cassandra nodes with different ports?

345 Views Asked by At

I am trying to connect to cluster nodes having different ports (9042 and 9043), but cannot find ways to do it here:

cluster = Cluster(['0.0.0.0','0.0.0.0'],port=9042)

How do I do it?

1

There are 1 best solutions below

0
Gino Mempin On

As of v3.27 of the DataStax's Python driver for Cassandra, Cluster's contact_points parameter seems to accept a tuple of IP and port.

https://github.com/datastax/python-driver/blob/3.27.0/cassandra/cluster.py#L581-L585

class Cluster(object):
    ...

    contact_points = ['127.0.0.1']
    """
    The list of contact points to try connecting for cluster discovery. A
    contact point can be a string (ip or hostname), a tuple (ip/hostname, port) or a
    :class:`.connection.EndPoint` instance. 

You can do

cluster = Cluster(
    contact_points=[
      ('0.0.0.0', 9042),
      ('0.0.0.0', 9043),
    ]
    ...
)

and then just leave the port= parameter as empty.