def connect(seeds, keyspace, datacenter=None, port=9042): class CustomRetryPolicy(RetryPolicy): def on_write_timeout(self, query, consistency, write_type, required_responses, received_responses, retry_num): # retry at most 5 times regardless of query type if retry_num >= 5: return (self.RETHROW, None) return (self.RETRY, consistency) load_balancing_policy = None if datacenter: # If you are using multiple datacenters it's important to use # the DCAwareRoundRobinPolicy. If not then the client will # make cross DC connections. This defaults to round robin # which means round robin across all nodes irrespective of # data center. load_balancing_policy = DCAwareRoundRobinPolicy(local_dc=DATACENTER) cluster = Cluster(contact_points=seeds, port=port, default_retry_policy=CustomRetryPolicy(), reconnection_policy=ExponentialReconnectionPolicy(1, 60), load_balancing_policy=load_balancing_policy, protocol_version=3) cluster.control_connection_timeout = 10.0 cluster.compression = False session = cluster.connect(keyspace) print 'Connection established with %s at port %s' % (seeds, port) return session
def connect(seeds, keyspace, datacenter=None, port=9042): from cassandra.io.libevreactor import LibevConnection from cassandra.cluster import Cluster from cassandra.policies import DCAwareRoundRobinPolicy, RetryPolicy, ExponentialReconnectionPolicy class CustomRetryPolicy(RetryPolicy): def on_write_timeout(self, query, consistency, write_type, required_responses, received_responses, retry_num): # retry at most 5 times regardless of query type if retry_num >= 5: return (self.RETHROW, None) return (self.RETRY, consistency) load_balancing_policy = None if datacenter: # If you are using multiple datacenters it's important to use # the DCAwareRoundRobinPolicy. If not then the client will # make cross DC connections. This defaults to round robin # which means round robin across all nodes irrespective of # data center. load_balancing_policy = DCAwareRoundRobinPolicy(local_dc=datacenter) cluster = Cluster(contact_points=seeds, port=port, auth_provider=auth_provider, default_retry_policy=CustomRetryPolicy(), reconnection_policy=ExponentialReconnectionPolicy(1, 60), load_balancing_policy=load_balancing_policy) cluster.connection_class = LibevConnection cluster.set_core_connections_per_host(0, 3) # local connections cluster.set_core_connections_per_host(1, 0) # remote connections cluster.control_connection_timeout = 10.0 cluster.compression = False session = cluster.connect(keyspace) return session