【问题标题】:Is Cassandra broken or have I made a huge mistake?Cassandra 是坏了还是我犯了一个巨大的错误?
【发布时间】: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


【解决方案1】:

你在调用

rs.all()

其中,as the javadoc says

将此 ResultSet 中的所有剩余行作为列表返回。

请注意,与iterator()连续调用one() 不同,这 方法强制一次获取 ResultSet 的全部内容, 特别是把这一切都记在心里。因此建议 如果可能,更喜欢通过iterator() 进行迭代,特别是如果 ResultSet 可以很大。

所以,当你这样做时

Row one = rs.one();

as the javadoc states 返回

如果此ResultSet 已用尽,则此结果集中的下一行或null

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

你会看到

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

在您的日志中显示您之前添加的行。

【讨论】:

  • @AlexWhite 是的,如果你在调用ResultSet#all() 之后调用ResultSet#isExhausted(),它将返回true。我不知道您所说的失败是什么意思。我删除了all() 调用和one() 调用,for 循环在使用单行后正确打印msg:broken db
  • @AlexWhite 不,我刚刚下载了 Cassandra 来尝试您的示例。你一直说事情没有用。编辑您的问题,发布您尝试运行的新代码和 exact 输出。然后告诉我们您的期望。
  • @AlexWhite 在您的编辑中,您仍在调用all(),它会消耗完整的ResultSet,一旦您到达for 循环,就什么都没有了。注意size 是 1。
猜你喜欢
  • 2022-10-13
  • 1970-01-01
  • 2013-01-01
  • 1970-01-01
  • 2018-12-22
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
  • 2010-12-31
相关资源
最近更新 更多