【问题标题】:Retrieving data from composite key via astyanax通过 astyanax 从复合键中检索数据
【发布时间】:2013-06-20 19:23:28
【问题描述】:

我对 cassandra 非常天真,并且正在使用 astyanax

CREATE TABLE 员工(empID int、deptID int、first_name varchar、 last_name varchar, PRIMARY KEY (empID, deptID));

我想获取查询的值: select * from employees where empID =2 and deptID = 800;

     public void read(Integer empID, String deptID) {
    OperationResult<ColumnList<String>> result;
    try {

        columnFamilies = ColumnFamily.newColumnFamily("employees", IntegerSerializer.get(), StringSerializer.get()); 
      result = keyspace.prepareQuery(columnFamilies).getKey(empID).execute();

      ColumnList<String> cols = result.getResult();
 //Other stuff

}

我应该如何做到这一点

【问题讨论】:

    标签: cassandra astyanax


    【解决方案1】:

    据我所知,没有一种超级干净的方法可以做到这一点。您必须通过执行 cql 查询然后遍历行来完成。此代码取自astynax examples file

    public void read(int empId) {
      logger.debug("read()");
      try {
        OperationResult<CqlResult<Integer, String>> result
          = keyspace.prepareQuery(EMP_CF)
            .withCql(String.format("SELECT * FROM %s WHERE %s=%d;", EMP_CF_NAME, COL_NAME_EMPID, empId))
            .execute();
        for (Row<Integer, String> row : result.getResult().getRows()) {
          logger.debug("row: "+row.getKey()+","+row); // why is rowKey null?
    
          ColumnList<String> cols = row.getColumns();
          logger.debug("emp");
          logger.debug("- emp id: "+cols.getIntegerValue(COL_NAME_EMPID, null));
          logger.debug("- dept: "+cols.getIntegerValue(COL_NAME_DEPTID, null));
          logger.debug("- firstName: "+cols.getStringValue(COL_NAME_FIRST_NAME, null));
          logger.debug("- lastName: "+cols.getStringValue(COL_NAME_LAST_NAME, null));
        }
      } catch (ConnectionException e) {
        logger.error("failed to read from C*", e);
        throw new RuntimeException("failed to read from C*", e);
      }
    }
    

    您只需调整 cql 查询以返回您想要的内容。这有点令人沮丧,因为根据文档,您可以这样做

    Column<String> result = keyspace.prepareQuery(CF_COUNTER1)
        .getKey(rowKey)
        .getColumn("Column1")
        .execute().getResult();
    Long counterValue = result.getLongValue();
    

    但是我不知道rowkey 是什么。我已经发布了a question about what rowkey can be。希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      相关资源
      最近更新 更多