0

I am very naive in cassandra & am using astyanax

CREATE TABLE employees (empID int, deptID int, first_name varchar, last_name varchar, PRIMARY KEY (empID, deptID));

i want to get the values of query: 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

}

how should i achieve this

S Kr
  • 1,831
  • 2
  • 25
  • 50

1 Answers1

0

As far as I can find, there isn't a super clean way to do this. You have to do it by executing a cql query and then iterating through the rows. This code is taken from the 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);
  }
}

You just have to tune the cql query to return what you want. This is a bit frustrating because according to the documentation, you can do

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

However I don't know what rowkey is. I've posted a question about what rowkey can be. Hopefully that will help

Community
  • 1
  • 1
Peter Klipfel
  • 4,958
  • 5
  • 29
  • 44