简体   繁体   English

如何使用Python驱动程序获取Cassandra集群的名称?

[英]How do I get the name of a Cassandra cluster, using the Python driver?

When I connect to Cassandra using cqlsh , it tells me the name of the Cassandra cluster I'm connected to. 当我使用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>

(In this example the cluster name is Test Cluster , and it appears in blue on my screen.) (在此示例中,群集名称为Test Cluster ,它在屏幕上显示为蓝色。)

How do I get the cluster name from within my Python code, using the Python Cassandra driver? 如何使用Python Cassandra驱动程序从Python代码中获取群集名称? I expected to be able to get this information from the Session object, but I cannot see anything in the documentation . 我希望能够从Session对象获取此信息,但是我在文档中看不到任何内容

(My use case is part of a belt-and-braces approach to prevent tests from being run against production Cassandras. The idea is that if the server name indicates that the tests have connected to a production Cassandra somehow, they can abort ASAP.) (我的用例是带括号的方法的一部分,以防止针对生产Cassandras进行测试。该想法是,如果服务器名称指示测试已以某种方式连接到生产Cassandra,则它们可以尽快中止。)

You can also find the cluster name (and other information) by querying the system.local table: 您还可以通过查询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()

Save that as getCluster.py and run it to see: 将其另存为getCluster.py并运行它以查看:

$ python getCluster.py 192.168.0.101 aaron flynnLives
AaronsHomeCluster

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python驱动程序在Cassandra的where子句中将日期时间用作群集? - How to use date time as a cluster in where clause in Cassandra using python driver? 如何使用cassandra-driver用于python将地图类型插入cassandra - How to insert map type into cassandra using cassandra-driver for python 使用python cassandra驱动程序连接到云cassandra - Connecting to cloud cassandra using python cassandra driver 如何使用python API Pydoop从Hadoop集群中获取实际数据(在缩小地图后)? - How do I get the actual data from Hadoop cluster (after map reducing) using the python API Pydoop? 如何使用cassandra python-driver实现基于令牌的分页? - How to implement token based pagination using cassandra python-driver? 如何使用 Python 将选项传递给 Selenium Chrome 驱动程序? - How do I pass options to the Selenium Chrome driver using Python? 如何使用Python连接到我的Amazon RedShift集群? - How do I connect to my Amazon RedShift cluster using Python? 如何使用 Selenium 驱动程序和 PYTHON 获得 http 响应? - How can i get http response using Selenium driver with PYTHON? 使用Python驱动程序为Cassandra创建表和索引 - Table and Index Creation Using Python Driver for Cassandra 在多线程中使用python cassandra驱动程序写入数据库 - using python cassandra driver in multithread to write to db
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM