【发布时间】:2014-11-14 23:30:42
【问题描述】:
我与 Cassandra 合作过很多次,感觉这些年来我已经忍受了来自数据库的足够多的 BS。只是想知道为什么这在带有 datastax java 驱动程序的 2.1.1 的 Apache Cassandra 2.1.0(或 2.0.8)中不起作用。看起来这应该可行。
public class BS {
public static void main(String [] args) {
Cluster cluster = Cluster.builder().addContactPoint("192.168.1.6").build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n", metadata.getClusterName());
Session session = cluster.connect();
session.execute("CREATE KEYSPACE IF NOT EXISTS fook WITH replication= {'class':'SimpleStrategy', 'replication_factor':1 }");
session.execute("CREATE TABLE IF NOT EXISTS fook.dooftbl (id bigint PRIMARY KEY, doof text, ownerid bigint)");
long id = new Random().nextLong();
long ownerid = new Random().nextLong();
String doof = "broken db";
String load = "INSERT INTO fook.dooftbl (id,doof,ownerid) VALUES (?,?,?)";
PreparedStatement ps = session.prepare(load);
session.execute(ps.bind(id,doof,ownerid));
try {
String cql = "SELECT doof FROM fook.dooftbl WHERE id=?";
PreparedStatement ps2 = session.prepare(cql);
ResultSet rs= session.execute(ps2.bind(id));
System.out.println("Result set: " + rs.toString() + " size: " + rs.all().size() + " fullyFetched:" + rs.isFullyFetched());
//Row one = rs.one();
//if (one!=null)
// System.out.println("It worked. You will never have to worry about seeing this msg.");
String msg = null;
for (Row r : rs.all()) {
msg = r.getString("doof");
}
System.out.println("msg:" + msg);
}
catch (Exception e) {
e.printStackTrace();
}
cluster.close();
}
}
我在这里做错了什么还是相对较小的事情?
输出:
连接到集群:测试集群
结果集:ResultSet[用尽:false,Columns[doof(varchar)]]大小:1fullyFetched:true
msg:null
【问题讨论】:
-
请描述不工作。你期望它做什么,为什么?它做了什么?
-
至少我希望它显示“它工作......”字符串。相反,它没有显示。
-
@AlexWhite 它显示什么?
Connected to cluster至少?
标签: java cassandra datastax nosql