Ale*_*ite -3 java cassandra nosql datastax
我和Cassandra工作了很多,感觉我多年来已经从数据库中拿出了足够的BS.只是想知道为什么这不适用于数据存储java驱动程序的Apache Cassandra 2.1.0(或2.0.8)w/2.1.1.看起来这应该是有用的.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里做错了还是相对较小的东西?
输出:
连接到群集:测试群集
结果集:ResultSet [耗尽:false,列[doof(varchar)]] size:1 fullyFetched:true
味精:空
你在调用
rs.all()
Run Code Online (Sandbox Code Playgroud)
其中,作为javadoc的说,
以列表形式返回此ResultSet中的所有剩余行.
请注意,与此相反
iterator()或连续调用one()相反,此方法强制立即获取ResultSet的完整内容,特别是将其全部保存在内存中.因此,建议iterator()在可能的情况下优先进行迭代,特别是如果ResultSet可能很大的话.
所以,当你这样做的时候
Row one = rs.one();
Run Code Online (Sandbox Code Playgroud)
其中,正如Javadoc,退货
此结果集中的下一行或者
null是否ResultSet已用完.
它会null在ResultSet耗尽后返回.
你会看到的
Result set: ResultSet[ exhausted: false, Columns[doof(varchar)]] size: 1 fullyFetched:true
Run Code Online (Sandbox Code Playgroud)
在您的日志中显示您之前添加的行.