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 Re: Querying CQL3 collections from Java
Date Mon, 25 Mar 2013 20:58:59 GMT
Thanks, this client library is really great! It should be included in the
Apache Cassandra Wiki's "High level clients" page (
http://wiki.apache.org/cassandra/ClientOptions).
Anyway, here is the code I created (using the cassandra-driver-core Maven
artifact).

Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").build();
Session session = cluster.connect("graph");
for (Row row : session.execute("SELECT * FROM vertices")) {
System.out.println("id: " + row.getString("id"));
System.out.println("properties: ");
Map<String, String> map = row.getMap("properties", String.class,
String.class);
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("- " + entry.getKey() + ": " + entry.getValue());
}
}

Gabor


On 25 March 2013 14:52, Sylvain Lebresne <sylvain@datastax.com> wrote:

> I would advise you not to use raw thrift. It's just a low-level transport
> as far as CQL3 is concerned, and what you will get is binary encoded data
> that you will have to decode manually. Use a client library (like
> https://github.com/datastax/java-driver) that will do that for you.
>
> Though to answer your initial question, the binary format used by map
> (that you will have to decode yourself) is described in
> https://git-wip-us.apache.org/repos/asf?p=cassandra.git;a=blob;f=doc/native_protocol.spec(Section
6).
>
> --
> Sylvain
>
>
> On Mon, Mar 25, 2013 at 2:30 PM, Szárnyas Gábor <szarnyasg@gmail.com>wrote:
>
>> 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