2015-09-11 83 views
2

我需要一些帮助来连接到ec2实例中的远程Cassandra数据库。让我们直接跳入。这里是我的Python脚本(我正在尝试两种不同的方法)。通过Python连接到远程Cassandra数据库

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');") 
''' 

这是我不断收到的错误。

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")}) 

这是我如何配置cassandra.yaml

listen_address: 172.XX.XX.XX 

它的设置机器的IP。

start_native_transport: true 
native_transport_port: 9042 

start_rpc: false 

rpc_address: 0.0.0.0 

broadcast_rpc_address: 172.XX.XX.XX 

也设置为机器的IP地址。

然后我做bin/cassandra -f并得到这个错误。

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

然后它宣布关机。

当我尝试bin/cassandra它说这个 -

INFO: No gossip backlog; proceeding 

然后,我已经等了15到20分钟,看它是否做任何事情......事实并非如此。

任何帮助表示赞赏!提前致谢!

+0

您的问题似乎是卡桑德拉没有成功启动,那么你应该看看在日志中看到为什么它的失败。看看:/var/log/cassandra/system.log –

+0

该文件不存在。 – NickTheInventor

+0

那里的目录?/var/log/cassandra –

回答

1

问题是,您正在使用旧版CQL驱动程序,它使用thrift/9160,而不是实现本机协议(9042)的新Python Python数据驱动程序驱动程序。

看:https://github.com/datastax/python-driver