在Windows操作系统下,使用DseAuthenticator和DseAuthorizer将Python连接到Cassandra集群

8
我已经尝试过使用 pycassacassandra.clusterdse.cluster,但都无法建立连接。

我感觉我正在连接到错误的主机,因为我只写了Linux服务器的主机名,没有指定任何关于Cassandra的信息。

同事告诉我他们只知道在Linux机器上通过 cqlsh 进行内联连接。这听起来很不方便。

cassandra.yaml 中有具体的配置。

authenticator: com.datastax.bdp.cassandra.auth.DseAuthenticator
authorizer: com.datastax.bdp.cassandra.auth.DseAuthorizer

我在Pycassa中所做的事情:

import pycassa
URIPORTLIST = ['12345.mycompany.net:9420']
pool = pycassa.ConnectionPool('my_keyspace', server_list=URIPORTLIST,credentials={'USERNAME':'fancycar','PASSWORD':'becauseimbatman'}, prefill=False)
cf = pycassa.ColumnFamily(pool, 'my_table')

错误信息:
AllServersUnavailable: An attempt was made to connect to each of the servers twice, but none of the attempts succeeded. The last failure was TTransportException: Could not connect to 12345.mycompany.net:9420

使用dse.cluster


from dse.cluster import Cluster
auth_provider = PlainTextAuthProvider(
        username='fancycar', password='becauseimbatman')
cluster = Cluster(
    ['12345.mycompany.net'],
    port=9042,auth_provider=auth_provider)
session = cluster.connect('my_keyspace')

错误信息:
NoHostAvailable: ('Unable to connect to any servers', {'11.111.11.1': AuthenticationFailed('Failed to authenticate to 11.111.11.2: Error from server: code=0100 [Bad credentials] message="Failed to login. Please re-try."',)})

1
尝试使用与cqlsh相同的主机和凭据连接到Cassandra,以查看是否可以正常工作... cqlsh 12345.mycompany.net -u fancycar -p becauseimbatman - undefined_variable
@undefined_variable 感谢您的建议。 这给了我一个错误: [root@12345.mycompany.net SYST:~]# cqlsh 12345.mycompany.net -u fancycar -p becauseimbatman 连接错误:('无法连接到任何服务器',{'11.111.11.11': error(None, "Tried connecting to [('11.111.11.11', 9042)]. Last error: timed out")}而以“通常”的方式登录是可以的:[root@12345.mycompany.net SYST:~]# cqlsh -u fancycar -p becauseimbatman已连接到12345_cluster,位于12345.mycompany.net:9042。 [cqlsh 5.0.1 | Cassandra 3.0.11.1485 | DSE 5.0.5 | CQL spec 3.4.0 | Native protocol v4] - MadsVJ
1
你的第二个命令连接到本地主机,而第一个连接到远程服务器。12345.mycompany.net是否解析为与localhost相同的主机? - undefined_variable
好的。我再试一次,现在你的@undefined_variable建议确实有效了。我的下一步应该是什么? - MadsVJ
您可能想在cassandra.yaml中查看rpc_address和listen_address。 - undefined_variable
为了从远程主机连接到Cassandra,必须确保两件事情:1)您应该能够从远程主机连接到Cassandra主机(telnet cassandra-host 9042)... 2)在cassandra.yaml中,rpc_address不应该是localhost。 - undefined_variable
1个回答

3

使用dse.auth的PlainTextAuthProvider而不是Cassandra AuthProvider进行修复。

from dse.cluster import Cluster
# pip install dse-driver
from dse.auth import PlainTextAuthProvider
auth_provider = PlainTextAuthProvider(
        username='fancycar', password='becauseimbatman ')
cluster = Cluster(contact_points=['12345.mycompany.net'],
port=9042, auth_provider=auth_provider)
session = cluster.connect('batcave')
print "connected"
print session.execute("SELECT * FROM robinstears")[0]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接