var cluster: Cluster = null
var session: Session = null
cluster = Cluster.builder().addContactPoints("192.168.1.3","192.168.1.2").build()
val metadata = cluster.getMetadata()
printf("Connected to cluster: %s\n",
metadata.getClusterName())
metadata.getAllHosts() map {
case host =>
printf("Datatacenter: %s; Host: %s; Rack: %s\n",
host.getDatacenter(), host.getAddress(), host.getRack())
}
我无法使用此代码连接到cassandra集群。它给了我错误 -
[NoHostAvailableException: All host(s) tried for query failed (tried: /192.168.1.3 ([/192.168.1.3] Cannot connect), /192.168.1.2 ([/192.168.1.2] Cannot connect))]
上述代码中的错误是什么。
答案 0 :(得分:3)
你的代码在第一次脸红时看起来不错。该错误表明Cassandra实际上并未在IP地址“192.168.1.3”,“192.168.1.2”上的端口9042(默认值)上运行
如果Cassandra在这些IP上运行,但它是另一个端口,则需要使用
int port = 19042; // Put the correct port here
cluster = Cluster.builder().addContactPoints("192.168.1.3","192.168.1.2").withPort(port).build()
答案 1 :(得分:0)
通过其thrift端口远程访问Cassandra(尽管注意JMX端口可用于执行一些有限的操作)。
thrift端口在cassandra.yaml中由rpc_port参数定义,默认为9160.您的cassandra节点应绑定到服务器网卡的IP地址 - 它不应该是127.0.0.1或localhost,它是环回接口的IP,绑定到此将阻止直接远程访问。使用cassandra.yaml中的rpc_address参数配置绑定地址。将其设置为0.0.0.0表示“在所有网络接口上监听”,这可能适合您,也可能不适合您。
要建立连接,您可以使用:
The cassandra-cli in the cassandra distribution's bin directory provides simple get / set / list operations and depends on Java
The cqlsh shell which provides CQL access to cassandra, this depends on Python
A higher level interface such as Apollo
答案 2 :(得分:0)
您可以使用端口9042并尝试连接ip本地主机或其他计算机,如下所示:
public String serverIP = "127.0.0.1"; //change ip with yours
//public String serverIP = "52.10.160.197"; //for prod
public String keyspace = "database name"; //for prod
//public String keyspace = "dbname_test"; //for staging
Cluster cluster = Cluster.builder().addContactPoint(serverIP).build();
Session session = cluster.connect(keyspace);