[英]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.