将 Cassandra 与 Python 连接的有效方法(抑制警告)

Effective way to connect Cassandra with Python (supress warnings)

我想使用 python 驱动程序从 Cassandra 数据库连接。

我可以使用 cassandra-driver 连接,但是当我 运行 pycharm:

中的代码时,我有几个警告
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider


server_config = {
                "host": "172.XX.XX.XX",
                "port": "9042",
                "user": "XXXXXXXXX",
                "password": "XXXXXXXX",
                "keySpace": "XXXXXX"
    }


keyspace = server_config['keySpace']

auth_provider = PlainTextAuthProvider(username=server_config['user'],password=server_config['password'])

node_ips = [server_config['host']]



cluster = Cluster(contact_points=node_ips, load_balancing_policy=None, port=int(server_config['port']), auth_provider=auth_provider, protocol_version=3)

session = cluster.connect()

session.set_keyspace(keyspace)


但是我有几个警告:

WARNING:cassandra.cluster:Cluster.__init__ called with contact_points specified, but no load_balancing_policy. In the next major version, this will raise an error; please specify a load-balancing policy. (contact_points = ['172.18.64.19'], lbp = None)

WARNING:cassandra.connection:An authentication challenge was not sent, this is suspicious because the driver expects authentication (configured authenticator = PlainTextAuthenticator)
INFO:cassandra.policies:Using datacenter 'datacenter1' for DCAwareRoundRobinPolicy (via host '172.18.64.19'); if incorrect, please specify a local_dc to the constructor, or limit contact points to local cluster nodes

WARNING:cassandra.connection:An authentication challenge was not sent, this is suspicious because the driver expects authentication (configured authenticator = PlainTextAuthenticator)

如何更有效地连接?

谢谢!

要避免这些警告,您应该遵循他们的建议。以下是关于它们的更多详细信息:

  1. 第一个建议指定负载均衡策略。在您的代码片段中,您将其配置为 None,这与根本不配置它是一样的。最好根据您的集群配置明确配置它。示例:

# This example assume that *datacenter1* if a valid DC for your cluster.
Cluster(contact_points=node_ips, 
        load_balancing_policy=DCAwareRoundRobinPolicy(local_dc='datacenter1'),
        port=int(server_config['port']), 
        auth_provider=auth_provider, 
        protocol_version=3)

  1. 第二个只是说您应该删除 auth_provider 配置,因为您要连接的 cluster/node 没有启用身份验证。