如何将用户名和密码传递给python中的cassandra

dra*_*hsu 10 python authentication cassandra

我正在学习并设置我的cassandra集群并尝试使用python作为客户端与之交互.在yaml中,我将authenticator设置为PasswordAuthenticator.

所以现在我计划将我的用户名和密码提供给connect函数,但是找不到放置它们的位置.

cluster = Cluster(hosts)
session = cluster.connect(keyspace)
Run Code Online (Sandbox Code Playgroud)

基本上,您只提供主机和密钥空间.文档类型建议与匿名连接? 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.',)})
Run Code Online (Sandbox Code Playgroud)

是否通过一些配置文件提供了身份验证信息?它似乎是一个非常基本的功能,我无法想象它不在python客户端驱动程序中.我一定错过了什么.

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

提前感谢任何暗示!

=================================================得到了它想通了.

我应该在auth_provider字段中提供用户名和密码,该字段是一个函数,返回包含"username"和"password"键以及相应字符串值的字典.

像def getCredential(self,host)之类的东西:credential = {'username':'myUser','password':'myPassword'}返回凭证

cluster = Cluster(nodes,auth_provider = getCredential)

bfb*_*bfb 14

按照操作建议使用:

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()
Run Code Online (Sandbox Code Playgroud)

如果您使用的是协议版本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()
Run Code Online (Sandbox Code Playgroud)

有人可以使用这些方法之一来解释使用远程集群进行身份验证的最佳实践吗?


小智 5

随您去。以下是从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()
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您使用协议版本 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)
Run Code Online (Sandbox Code Playgroud)