2013-05-28 44 views
22

在連接到客戶端卡桑德拉用java驅動器,用於通過Cannsandra DataStax,則投擲以下錯誤..錯誤而連接到卡桑德拉使用Java驅動爲Apache卡桑德拉1.0從com.example.cassandra

異常在線程「主」 com.datastax.driver.core.exceptions.NoHostAvailableException:所有主機(S)試了查詢失敗(嘗試:[/127.0.0.1])

請建議...

謝謝!

我的Java代碼是這樣的:

package com.example.cassandra; 

import com.datastax.driver.core.Cluster; 
import com.datastax.driver.core.Host; 
import com.datastax.driver.core.Metadata; 

public class SimpleClient { 

private Cluster cluster; 

public void connect(String node){ 

    cluster = Cluster.builder().addContactPoint(node).build(); 
    Metadata metadata = cluster.getMetadata(); 
    System.out.println(metadata.getClusterName()); 
} 


public void close() 
{ 
cluster.shutdown(); 
} 

public static void main(String args[]) { 

SimpleClient client = new SimpleClient(); 
client.connect("127.0.0.1"); 
client.close(); 
} 
+1

只是爲了增加更多的清晰度:我相信我連接到cassandra。它仍然顯示這個錯誤 –

回答

4

轉到你的Apache Cassandra的conf目錄下,並啓用二進制協議

卡桑德拉二進制協議 的Java驅動程序使用的是對卡桑德拉推出的二進制協議1.2。它只適用於大於或等於1.2的Cassandra版本。此外,二進制協議服務器未使用Cassandr a 1.2中的默認配置文件啓動。您必須編輯每個節點的cassandra.yaml文件:

start_native_transport: true 

然後重新啓動節點。

10

就我而言,我遇到了這個問題,因爲我在連接過程中使用了默認的RPC端口9160。人們可以找到不同的端口CQL在cassandra.yaml -

start_native_transport: true 
# port for the CQL native transport to listen for clients on 
native_transport_port: 9042 

有一次,我改變了代碼使用端口9042連接嘗試成功 -

public BinaryDriverTest(String cassandraHost, int cassandraPort, String keyspaceName) { 
    m_cassandraHost = cassandraHost; 
    m_cassandraPort = cassandraPort; 
    m_keyspaceName = keyspaceName; 

    LOG.info("Connecting to {}:{}...", cassandraHost, cassandraPort); 
    cluster = Cluster.builder().withPort(m_cassandraPort).addContactPoint(cassandraHost).build(); 
    session = cluster.connect(m_keyspaceName); 
    LOG.info("Connected."); 
} 

public static void main(String[] args) { 
    BinaryDriverTest bdt = new BinaryDriverTest("127.0.0.1", 9042, "Tutorial"); 
} 
2

在我來說,這是一個端口號,我忘了更新

舊RPC端口是9160
新的二進制端口9042

5

我有這個問題,它是由在SocketOptions設置ReadTimeout分類:

Cluster cluster = Cluster.builder().addContactPoint("localhost").build(); 
cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(HIGHER_TIMEOUT); 
3

只是張貼這對於像我一樣誰可能有同樣的問題,當我得到這個錯誤消息的人。原來我的複雜依賴關係樹帶來了舊版本的com.google.collections,這打破了CQL驅動程序。消除這種依賴性,完全依靠番石榴解決了我的問題。

+1

感謝您的提示,它解決了我的問題 – Harsha

3

我遇到同一個問題,測試一個節點的新集羣。

從羣集建設者刪除此之後,我能夠連接:

.withLoadBalancingPolicy(new DCAwareRoundRobinPolicy("US_EAST")) 

它能夠連接。

4

我也有同樣的問題。我已經在一個單獨的Linux PC中安裝了Cassandra,並嘗試通過Window PC進行連接。我不被允許創建連接。

但是當我們編輯cassandra。YAML,把我的Linux PC的IP地址rpc_address並重新啓動,它讓我成功連接,

# The address or interface to bind the Thrift RPC service and native transport 
# server to. 
# 
# Set rpc_address OR rpc_interface, not both. Interfaces must correspond 
# to a single address, IP aliasing is not supported. 
# 
# Leaving rpc_address blank has the same effect as on listen_address 
# (i.e. it will be based on the configured hostname of the node). 
# 
# Note that unlike listen_address, you can specify 0.0.0.0, but you must also 
# set broadcast_rpc_address to a value other than 0.0.0.0. 
#rpc_address: localhost 
rpc_address: 192.168.0.10 
2

我也遇到了這個問題,它是由在聲明一個簡單的錯誤,目前正在提交所致。

session.prepare(null); 

顯然,錯誤信息是誤導性的。

1

編輯

/etc/cassandra/cassandra.yaml 

,改變

rpc_address0.0.0.0,broadcast_rpc_address和listen_address向IP集羣的地址。

0

檢查以下幾點:

我)檢查服務器的IP

二)檢查監聽端口

III)數據堆棧客戶的依賴必須與服務器版本相匹配。

關於YAML文件,最新版本低於屬性中啓用:

start_native_transport: true 
    native_transport_port: 9042 
0

假設你已經到位默認配置,檢查驅動程序版本的兼容性。並非所有的驅動程序版本都與Cassandra的所有版本兼容,儘管它們聲稱具有向後兼容性。請參閱下面的鏈接。

http://docs.datastax.com/en/developer/java-driver/3.1/manual/native_protocol/

我遇到了類似的問題&改變驅動程序版本解決了我的問題。

注意:希望您使用Maven(或類似的東西)來解決依賴關係。否則,您可能不得不下載驅動程序更高版本的許多依賴關係。