代码之家  ›  专栏  ›  技术社区  ›  dave4420

如何使用python驱动程序获得cassandra集群的名称?

  •  1
  • dave4420  · 技术社区  · 6 年前

    当我用 cqlsh ,它告诉我所连接的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

    (我的用例是belt-and-braces方法的一部分,用于防止对生产cassandras运行测试。其思想是,如果服务器名称指示测试以某种方式连接到生产cassandra,则它们可以尽快中止。)

    2 回复  |  直到 6 年前
        2
  •  2
  •   Aaron    6 年前

    您还可以通过查询 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