【问题标题】:How do I get the name of a Cassandra cluster, using the Python driver?如何使用 Python 驱动程序获取 Cassandra 集群的名称?
【发布时间】:2018-06-15 22:37:06
【问题描述】:

当我使用 cqlsh 连接到 Cassandra 时,它会告诉我我连接到的 Cassandra 集群的名称。

$ cqlsh
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh>

(在本例中,集群名称为Test Cluster,它在我的屏幕上显示为蓝色。)

如何使用 Python Cassandra 驱动程序从我的 Python 代码中获取集群名称?我希望能够从Session 对象中获取此信息,但我在the documentation 中看不到任何内容。

(我的用例是防止测试针对生产 Cassandra 运行的安全带和大括号方法的一部分。其想法是,如果服务器名称表明测试已以某种方式连接到生产 Cassandra,则它们可以中止尽快。)

【问题讨论】:

    标签: python cassandra


    【解决方案1】:

    【讨论】:

    • 详细说明:这可通过Cluster 获得,例如cluster.metadata.cluster_name,不是通过Session,而是只有在你创建了会话之后才可用。
    【解决方案2】:

    您还可以通过查询system.local 表找到集群名称(和其他信息):

    from cassandra.cluster import Cluster
    from cassandra.auth import PlainTextAuthProvider
    import sys
    
    hostname=sys.argv[1]
    username=sys.argv[2]
    password=sys.argv[3]
    
    nodes = []
    nodes.append(hostname)
    auth_provider = PlainTextAuthProvider(username=username, password=password)
    ssl_opts = {'ca_certs':'/home/aaron/.cassandra/mycert.pem'}
    cluster = Cluster(nodes,auth_provider=auth_provider,ssl_options=ssl_opts)
    session = cluster.connect()
    
    strCQL = "SELECT cluster_name FROM system.local"
    pStatement = session.prepare(strCQL)
    rows = session.execute(pStatement)
    
    for row in rows:
        print row[0]
    
    session.shutdown()
    

    将其另存为getCluster.py并运行查看:

    $ python getCluster.py 192.168.0.101 aaron flynnLives
    AaronsHomeCluster
    

    【讨论】:

      猜你喜欢
      • 2013-03-02
      • 2020-01-01
      • 1970-01-01
      • 2014-03-08
      • 2019-04-04
      • 1970-01-01
      • 2018-01-05
      • 2018-04-12
      • 2016-07-26
      相关资源
      最近更新 更多