我想使用以下代码连接到我的本地cassandra数据库:
public class StartDriver {
public static void main(String[] args) {
Cluster cluster = null;
try {
cluster = Cluster.builder() // (1)
.addContactPoint("127.0.0.1").withPort(9042).build();
SocketOptions socketOptions = cluster.getConfiguration().getSocketOptions();
socketOptions.setConnectTimeoutMillis(1000);
System.out.println("Connection established");
Session session = cluster.connect(); // (2)
System.out.println(session.getState().getConnectedHosts());
System.out.println("Try to execute Query");
ResultSet rs = session.execute("SELECT * FROM htw.student");
System.out.println("Query executed");// (3)
Row row = rs.one();
// System.out.println(row.getString("release_version"));
if (cluster != null) cluster.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
可以建立连接,但是在session.execute(),程序永远挂起,没有任何错误消息。我在windows下使用Cassandra 3.8.0。通过cqlsh和devCenter连接是可能的,并且工作得很好。有人可以帮忙吗?在cassandra.yaml中是否有任何配置需要改变?
我得到的输出如下:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Connection established
[/127.0.0.1:9042]
Try to execute Query
答案 0 :(得分:0)
运行此代码,请告诉我您的输出。
public class StartDriver {
public static void main(String[] args) {
Cluster cluster = null;
try {
cluster = Cluster.builder() // (1)
.addContactPoint("127.0.0.1").withPort(9042).build();
SocketOptions socketOptions = cluster.getConfiguration().getSocketOptions();
socketOptions.setConnectTimeoutMillis(1000);
System.out.println("Connection established");
Session session = cluster.connect(); // (2)
System.out.println(session.getState().getConnectedHosts());
System.out.println("Try to execute Query");
ResultSet rs = session.execute("SELECT count(*) FROM htw.student");
System.out.println("Query executed");// (3)
Row row = rs.one();
System.out.println(row.getLong("count"));
if (cluster != null) cluster.close();
} catch (Exception e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
问题是在类路径中有一些lib就像guava,它们来自cassandra-driver-core。我将projekt转换为maven projekt,从这一点来看,lib似乎相互阻碍。我从libs文件夹中删除了lib',只使用了maven depnendencies,而不是像魅力一样的wirking。