gpt4 book ai didi

java - 如何使用 Java 类在 Cassandra 中创建 KEYSPACE

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:25:59 24 4
gpt4 key购买 nike

我是 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

最佳答案

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

Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException

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

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

cluster = Cluster.builder().addContactPoint("127.0.0.1").build();

关于java - 如何使用 Java 类在 Cassandra 中创建 KEYSPACE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16940595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com