2014-02-14 60 views
7

我正在学习并刚刚安装我的cassandra集群,并尝试使用python作为客户端与它进行交互。在yaml中,我将验证器设置为PasswordAuthenticator。如何在python中将用户名和密码传递给cassandra

所以现在我打算提供我的用户名和密码到连接功能,但找不到放置它们的位置。

cluster = Cluster(hosts) 
session = cluster.connect(keyspace) 

基本上,你只提供主机和密钥空间。文档类型建议与匿名连接? http://datastax.github.io/python-driver/getting_started.html#connecting-to-cassandra

如果我只是使用的例子,我会收到以下错误

raise NoHostAvailable("Unable to connect to any servers", errors) 
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'hou722067': AuthenticationFailed('Remote end requires authentication.',)}) 

是通过一些配置文件提供的认证信息?它看起来像一个非常基本的功能,我无法想象它不包含在python客户端驱动程序中。我一定错过了什么。

总之,我的问题是:如何使用python登录cassandra?

在此先感谢您的任何提示!

============================================== === 明白了。

我应该在auth_provider字段中提供用户名和密码,该字段是一个函数,返回包含具有适当字符串值的“用户名”和“密码”键的字典。

喜欢的东西 DEF getCredential(个体,主机): 凭证= { '的用户名': 'MYUSER', '密码': 'MYPASSWORD'} 返回凭证

簇=群集(节点,auth_provider = getCredential)

+1

如果你已经找到了解决您的问题,那么请张贴和接受它作为答案。这将使其他用户知道这个问题已经解决。 –

+0

我是一个新成员,系统不会让我在10天左右回答自己的问题。这就是为什么我只是在更新后的帖子。一旦系统允许我这样做,我会回答。 – drakihsu

回答

13

继OPS推荐使用:

from cassandra.cluster import Cluster 

def getCredential(self): 
    return {'username': 'foo', 'password': 'bar'} 

node_ips = ['0.0.0.0', '0.0.0.1'] 

cluster = Cluster(node_ips, protocol_version=1, auth_provider=getCredential) 
session = cluster.connect() 

如果您使用协议版本2,那么你必须用AuthProvider对象进行身份验证。 EX:

from cassandra.auth import PlainTextAuthProvider 
from cassandra.cluster import Cluster 

ap = PlainTextAuthProvider(username=foo, password=bar) 
c = Cluster(protocol_version=2, auth_provider=ap) 
s = c.connect() 

有人可以解释使用这两种方法之一来验证远程集群的最佳做法吗?

+0

嘿,感谢@ bfb这为我工作,但只是一个注意,非关键字参数应该是所有关键字之前 –

1

你在这里.. 下面是从python连接cassandra的代码。

from cassandra.cluster import Cluster 
from cassandra.auth import PlainTextAuthProvider 
auth_provider = PlainTextAuthProvider(username='username', password='password') 
cluster = Cluster(["hostname"],auth_provider = auth_provider) 
session = cluster.connect() 
session.set_keyspace('keyspace') 
cluster.connect() 
1

如果您使用协议版本4,那么你必须用AuthProvider对象进行身份验证,确定先前auth_provider

from cassandra.auth import PlainTextAuthProvider 
from cassandra.cqlengine import connection 

    auth_provider = PlainTextAuthProvider(username='user_name', password='user_pwd') 

    connection.setup(cassandra_host, default_keyspace, retry_connect=True, protocol_version=4, auth_provider=auth_provider) 
相关问题