访问Cassandra
Cassandra集群中的机器没有主从或者线性关系,分布式的集群可以通过访问集群中任一机器的cassandra来连接cassandra集群。
Cassandra 集群可以有无数个节点
cqlsh连接cassandra
[[email protected] ~]# cqlsh -u username -pdbname ip1
Connected to DBS Cluster at ip1:9042.
[cqlsh 5·0·1 | Cassandra 3·10 | CQL spec 3·4·4 | Native protocol v4]
Use HELP for help.
cqlsh> select cluster_name, listen_address from system.local;
cluster_name | listen_address
DBS Cluster | ip1
(1 rows)
cqlsh>
Python连接cassandra
在Cassandra 集群部署(二)——Ntpd和Python、Java安装文章中我们介绍了python pip安装命令pip install cassandra-drive安装Python访问Cassandra的驱动包,现在我们在命令行运行python直接访问测试
[[email protected] ~]# python
Python 2.7.13 (default, Jun 2 2017, 10:12:53)
[GCC 4.4.7 20120313 (Red Hat 4·4·7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from cassandra.cluster import Cluster
>>> from cassandra.auth import PlainTextAuthProvider
>>> cluster = Cluster(['ip1'],auth_provider=PlainTextAuthProvider(username='username', password='password'))
>>> session = cluster.connect('system')
>>> rows = session.execute('select cluster_name, listen_address from local')
>>> for row in rows:
... print(row)
Row(cluster_name=u'DBS Cluster', listen_address='ip1')