Ravaal
Ravaal

Reputation: 3359

Connecting to a remote Cassandra Database through Python

I need some help making a connection to a remote Cassandra database housed on an ec2 instance. Let's just dive right in. Here's my python script (I'm trying two different methods).

import cql
from cassandra.cluster import Cluster

cluster = Cluster(contact_points = ['172.XX.XX.XX',], port=<port>)
session = cluster.connect('demo')

while True:
    fname = raw_input('First name> ')
    lname = raw_input('Last name> ')
    session.execute("INSERT INTO demo (firstname, lastname) VALUES ('"+fname+"', '"+lname+"')")


'''connection = cql.connect('172.XX.XX.XX', <port>, 'demo')
print 'made connection'
cur = connection.cursor()
cur.execute("INSERT INTO example (firstname, lastname) VALUES ('Guy', 'Man');")
'''

This is the error I keep getting.

Traceback (most recent call last):
File "/home/user/Cassandra/remote connect.py", line 5, in <module>
session = cluster.connect('demo')
File "build/bdist.linux-x86_64/egg/cassandra/cluster.py", line 840, in connect
self.control_connection.connect()
File "build/bdist.linux-x86_64/egg/cassandra/cluster.py", line 2087, in connect
self._set_new_connection(self._reconnect_internal())
File "build/bdist.linux-x86_64/egg/cassandra/cluster.py", line 2122, in _reconnect_internal
raise NoHostAvailable("Unable to connect to any servers", errors)
NoHostAvailable: ('Unable to connect to any servers', {'172.XX.XX.XX': error(None, "Tried connecting to [('172.XX.XX.XX', <port>)]. Last error: None")})

This is how I configured cassandra.yaml

listen_address: 172.XX.XX.XX

It's set to the IP of the machine.

start_native_transport: true
native_transport_port: 9042

start_rpc: false

rpc_address: 0.0.0.0

broadcast_rpc_address: 172.XX.XX.XX

Set to the IP of the machine as well.

Then I do bin/cassandra -f and get this error.

java.lang.IllegalStateException: Failed to bind port 9042 on 0.0.0.0.

Then it announces shutdown.

When I try bin/cassandra it says this-

INFO: No gossip backlog; proceeding

And then I've waited for 15 to 20 minutes to see if it does anything... it doesn't.

Any help is appreciated! Thanks in advance!

Upvotes: 2

Views: 4345

Answers (1)

Jeff Jirsa
Jeff Jirsa

Reputation: 4426

The problem is that you're using the legacy CQL driver, which uses thrift/9160, rather than the new Python datastax driver which implements the native protocol (9042).

Look at: https://github.com/datastax/python-driver

Upvotes: 1

Related Questions