我是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
答案 0 :(得分:2)
为什么您的主机“/127.0.0.1”而不只是“127.0.0.1”?
线程“main”中的异常com.datastax.driver.core.exceptions。 NoHostAvailableException
错误告诉您连接未正确建立,因为驱动程序无法连接到您提供的任何提供的节点(String节点)。
为了让它变得非常简单 ...尝试:
cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
答案 1 :(得分:1)
问题由以下原因引起:Caused by: com.datastax.driver.core.exceptions.NoHostAvailableException
@Lyuben Todorov,问题不是由/127.0.0.1
中的前导斜杠引起的。
根据此question,在IP之前存在前导/
的原因是记录器隐式调用toString
的{{1}}。
我认为原因可能是Cassandra远程设置或Cassandra设置的防火墙设置。
对于Cassandra设置:更改InetAddress
并重新启动Cassandra:
/etc/cassandra/cassandra.yaml
对于防火墙设置:
rpc_address: 0.0.0.0
broadcast_rpc_address: {YOUR_SERVER_ID_HERE}
有关详细信息,请参阅此question
答案 2 :(得分:0)
我遇到了与OP相同的问题。这会发生什么,如果我从命令行运行cassandra-stress,我得到输出为 Datatacenter:datacenter1;主持人:localhost / 127.0.0.1;机架:rack1 即,您可以看到主机被指定为localhost或127.0.0.1。 当我使用Eclipse运行测试时,我看到正斜杠出现了。 所以现在,读取节点参数并截断正斜杠可能是个更好的主意。