cassandra-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tamar Fraenkel <ta...@tok-media.com>
Subject get all columns for a row
Date Mon, 23 Jan 2012 08:24:19 GMT
Hi!

I am getting started with Cassandra and Hector and have a question.
If I have CF with many columns, only a small portion of them having values for
any given key. I want to read all columns for a given key, but I don't know
which columns were populated.
I managed to do that with the code below (assumes that column names are
incremental numbers).
Is there any better way?
Thanks,
Tamar
 
private static void readAllColunsForRowKey(String key) {
  MultigetSliceQuery<String, Long, String> multigetSlicesQuery =
    HFactory.createMultigetSliceQuery(tutorialKeyspace, StringSerializer.get(),
LongSerializer.get(),
       StringSerializer.get());
  multigetSlicesQuery.setColumnFamily(CF);
  multigetSlicesQuery.setKeys(key);
  multigetSlicesQuery.setRange(null, null, false, COL_COUNT);
  QueryResult<Rows<String, Long, String>> results =
multigetSlicesQuery.execute();
  Long lastColName;
  while ((lastColName = printColsAndGetLastColName(results, key)) != null) {
    multigetSlicesQuery.setRange(lastColName + 1, null, false, COL_COUNT);
    results = multigetSlicesQuery.execute();
  }
}

private static Long printColsAndGetLastColName(QueryResult<Rows<String, Long,
String>> results, String key) {
  Rows<String, Long, String> rows = results.get();
  if (rows.getCount() != 1) {
    System.out.println("No such key");
    return null;
  }
  Row<String, Long, String> row = rows.getByKey(key);
  ColumnSlice<Long, String> columnSlice = row.getColumnSlice();
  List<HColumn<Long, String>> columns = columnSlice.getColumns();
  for (HColumn<Long, String> column : columns) {
    System.out.println("Column Name: " + column.getName()
      + ", Column Val: " + column.getValue());
  }
  if (columns.size() == COL_COUNT) {
    return columns.get(COL_COUNT - 1).getName();
  }
  return null;
}
Mime
View raw message