卡桑德拉是坏了还是我犯了一个大错误?

问题描述:

我已经和Cassandra合作过很多年了,觉得我多年来已经忍受了来自数据库的足够的BS。只是想知道为什么这不适用于DataStore 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(); 
    } 

} 

我在这里做错了什么,还是比较小的东西?

输出:

连到群集:测试群集

结果集:结果集[耗尽:假,列[笨蛋(VARCHAR)]]尺寸:1 fullyFetched:真

MSG :空

+0

请描述_isn't working_。你期望它做什么,为什么?它做什么呢? – 2014-09-20 14:19:42

+0

最低限度,我希望它能显示“It worked ...”字符串。相反,它没有显示它。 – 2014-09-20 14:25:37

+0

@AlexWhite它显示了什么_does_?至少连接到集群? – 2014-09-20 14:57:13

你调用

rs.all() 

其中,as the javadoc says

返回在这个ResultSet作为一个列表中的其余行。

需要注意的是,违背iterator()连续调用one()这种 方法强制一次, 获取ResultSet中的全部内容保持一切在内存中尤其如此。因此建议 在可能的情况下更喜欢通过iterator()的迭代,特别是如果 ResultSet可能很大。

所以,当你然后做

Row one = rs.one(); 

其中,as the javadoc states,如果这ResultSet耗尽返回

下一行此ResultSet或null

它会返回null,因为ResultSet已用尽。

你会在你的记录,显示您以前添加该行已经看到

Result set: ResultSet[ exhausted: false, Columns[doof(varchar)]] size: 1 fullyFetched:true 

+0

@AlexWhite是的,如果您在调用ResultSet#all()后调用ResultSet#isExhausted(),它将返回true。我不知道_fails_是什么意思。我删除了'all()'调用和'one()'调用,'for'循环在消耗单行之后正确地打印了'msg:broken db'。 – 2014-09-20 16:02:30

+0

@AlexWhite不,我刚刚下载Cassandra来尝试你的例子。你一直说事情不行。编辑您的问题,发布您尝试运行的新代码和_exact_输出。然后告诉我们你的期望。 – 2014-09-20 16:08:47

+0

@AlexWhite从编辑中,您仍然调用'all()',它会使用完整的'ResultSet',一旦到达'for'循环就没有任何东西。注意'size'是多少1。 – 2014-09-20 16:15:48