incubator-cassandra-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Szárnyas Gábor <szarny...@gmail.com>
Subject Querying CQL3 collections from Java
Date Mon, 25 Mar 2013 13:30:06 GMT
Hello!

I got stuck when trying to query CQL3 collections from Java.
I'm using Cassandra 1.2.3 with CQL3. I created a column family to store a
property graph's edges with the following command:

CREATE TABLE vertices (
  id text PRIMARY KEY,
  properties map<text, text>
)

I can access the data from the cqlsh, however, I couldn't figure out how to
iterate through the map entries in Java.
The following code iterates through the rows and columns, but does not
retrieve the key-value pairs of the "properties" map.

String query = "SELECT * FROM vertices";
CqlResult cqlResult =
client.execute_cql3_query(ByteBuffer.wrap(query.getBytes()),
COMPRESSION_LEVEL.NONE, CONSISTENCY_LEVEL.ALL);

Iterator<CqlRow> rowsIterator = cqlResult.getRowsIterator();
while (rowsIterator.hasNext()) {
  CqlRow cqlRow = rowsIterator.next();
  Iterator<Column> columnsIterator = cqlRow.getColumnsIterator();
  while (columnsIterator.hasNext()) {
    Column cqlColumn = columnsIterator.next();

    byte[] name = cqlColumn.getName();
    String nameString = new String(name);
    System.out.print(nameString + ": ");

    byte[] value = cqlColumn.getValue();
    String string = new String(value);
    System.out.println(string);
  }
}

The cqlResult.getSchema() method shows the column with the type
"org.apache.cassandra.db.marshal.MapType(org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.UTF8Type)".
How can I create a HashMap<String, String> from each row's properties cell?

Thanks,
Gabor

Mime
View raw message