6

我是 Cassandra 的新手,我想在 Cassandra 中进行 CRUD 操作。我能够从 java 类连接 Cassandra。但是现在当我做 CRUD 时它不起作用。这是我的代码..

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

public class AnotherClient {

private Session session;

private Cluster cluster;

public void connect(String node) {
    cluster = Cluster.builder().addContactPoint(node).build();
    Metadata metadata = cluster.getMetadata();
    System.out.println("Cassandra connection established");
    System.out.printf("Connected to cluster: %s\n",
            metadata.getClusterName());
    for (Host host : metadata.getAllHosts()) {
        System.out.printf("Datatacenter: %s; Host: %s; Rack: %s \n",
                host.getDatacenter(), host.getAddress(), host.getRack());
        session = cluster.connect();

    }
}

public void createSchema() {
    session.execute("CREATE KEYSPACE simplex WITH replication "
            + "= {'class':'SimpleStrategy', 'replication_factor':3};");

    session.execute("CREATE TABLE simplex.songs (" + "id uuid PRIMARY KEY,"
            + "title text," + "album text," + "artist text,"
            + "tags set<text>," + "data blob" + ");");
    session.execute("CREATE TABLE simplex.playlists (" + "id uuid,"
            + "title text," + "album text, " + "artist text,"
            + "song_id uuid," + "PRIMARY KEY (id, title, album, artist)"
            + ");");

}

public void loadData() {
    session.execute("INSERT INTO simplex.songs (id, title, album, artist, tags) "
            + "VALUES ("
            + "756716f7-2e54-4715-9f00-91dcbea6cf50,"
            + "'La Petite Tonkinoise',"
            + "'Bye Bye Blackbird',"
            + "'Joséphine Baker'," + "{'jazz', '2013'})" + ";");
    session.execute("INSERT INTO simplex.playlists (id, song_id, title, album, artist) "
            + "VALUES ("
            + "2cc9ccb7-6221-4ccb-8387-f22b6a1b354d,"
            + "756716f7-2e54-4715-9f00-91dcbea6cf50,"
            + "'La Petite Tonkinoise',"
            + "'Bye Bye Blackbird',"
            + "'Joséphine Baker'" + ");");
}

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

public static void main(String[] args) {
    AnotherClient client = new AnotherClient();
    client.connect("127.0.0.1");

    client.createSchema();
    client.close();
}

}

连接已正确建立。但 createSchema() 方法无法正常工作。这是我的错误日志。

Cassandra connection established
Connected to cluster: Test Cluster
Datatacenter: datacenter1; Host: /127.0.0.1; Rack: rack1 
Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: [])
    at com.datastax.driver.core.exceptions.NoHostAvailableException.copy(NoHostAvailableException.java:61)
    at com.datastax.driver.core.ResultSetFuture.extractCauseFromExecutionException(ResultSetFuture.java:234)
    at com.datastax.driver.core.ResultSetFuture.getUninterruptibly(ResultSetFuture.java:165)
    at com.datastax.driver.core.Session.execute(Session.java:106)
    at com.datastax.driver.core.Session.execute(Session.java:75)
    at com.example.cassandra.AnotherClient.createSchema(AnotherClient.java:30)
    at com.example.cassandra.AnotherClient.main(AnotherClient.java:81)
Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: [])
    at com.datastax.driver.core.RequestHandler.sendRequest(RequestHandler.java:93)
    at com.datastax.driver.core.Session$Manager.execute(Session.java:393)
    at com.datastax.driver.core.Session$Manager.executeQuery(Session.java:429)
    at com.datastax.driver.core.Session.executeAsync(Session.java:146)
    ... 4 more
4

3 回答 3

2

为什么您的主机是 "/127.0.0.1" 而不仅仅是 "127.0.0.1" ?

线程“主”com.datastax.driver.core.exceptions 中的异常。NoHostAvailableException

该错误告诉您连接未正确建立,因为驱动程序无法连接到您作为参数(字符串节点)提供的任何提供的节点。

为了让它变得非常简单......尝试:

cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
于 2013-06-05T14:20:41.807 回答
1

问题是由以下原因引起的:Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException

@Lyuben Todorov,问题不是由/127.0.0.1. 根据这个问题,IP前有一个前导的原因/是记录器隐式调用toString的。InetAddress

我认为原因可能是 Cassandra 远程设置或 Cassandra 设置的防火墙设置。

  1. 对于 Cassandra 设置:更改/etc/cassandra/cassandra.yaml并重新启动 Cassandra:

    rpc_address: 0.0.0.0 broadcast_rpc_address: {YOUR_SERVER_ID_HERE}

  2. 对于防火墙设置:

    ufw allow 9042 ufw status

有关更多信息,请参阅此问题

于 2018-06-10T03:53:45.500 回答
0

我遇到了与OP相同的问题。会发生什么,如果我从命令行运行 cassandra-stress,我会得到输出为 Datatacenter: datacenter1; 主机:localhost/127.0.0.1;Rack: rack1 即你可以看到主机被指定为 localhost 或 127.0.0.1。当我使用 Eclipse 运行测试时,我看到出现了正斜杠。所以现在,读取节点参数并截断正斜杠可能是一个更好的主意。

于 2015-06-24T06:00:15.100 回答