Monday, October 8, 2012

CQL, Astyanax and Compound/Composite Keys: Writing Data


In my previous post, I showed how to connect the dots between Astyanax and CQL, but it focused primarily on reading.  Here is the update that connects the dots on write.

I created a sample project and put it out on github.  Let me know if you have any trouble.

Extending the previous example to accomodate writes, you need to use the AnnotatedCompositeSerializer when writing as well.  Here is the code:


    public void writeBlog(String columnFamilyName, String rowKey, FishBlog blog, byte[] value) throws ConnectionException {
        AnnotatedCompositeSerializer entitySerializer = new AnnotatedCompositeSerializer(FishBlog.class);
        MutationBatch mutation = keyspace.prepareMutationBatch();
        ColumnFamily columnFamily = new ColumnFamily(columnFamilyName,
                StringSerializer.get(), entitySerializer);
        mutation.withRow(columnFamily, rowKey).putColumn(blog, value, null);
        mutation.execute();
    }

Now, if you recall from the previous post, we had a single object, FishBlog, that represented the compound/composite column name:

public class FishBlog {
    @Component(ordinal = 0)
    public long when;
    @Component(ordinal = 1)
    public String fishtype;
    @Component(ordinal = 2)
    public String field;
}

We mapped this object to the following schema:
 
    CREATE TABLE fishblogs (
        userid varchar,
        when timestamp,
        fishtype varchar,
        blog varchar,
        image blob,
        PRIMARY KEY (userid, when, fishtype)
    );

We had one member variable in FishBlog, field, that specified which piece of data we were writing: image or blog.  Because of how things work with CQL and the Java API, you actually need *two* mutations to create a row.  Here is the code from the unit test:

       AstyanaxDao dao = new AstyanaxDao("localhost:9160", "examples");
       FishBlog fishBlog = new FishBlog();
       fishBlog.fishtype="CATFISH";
       fishBlog.field="blog";
       fishBlog.when=now;
       dao.writeBlog("fishblogs", "bigcat", fishBlog, "myblog.".getBytes());
       
       FishBlog image = new FishBlog();
       image.fishtype="CATFISH";
       image.field="image";
       image.when = now;
       byte[] buffer = new byte[10];
       buffer[0] = 1;
       dao.writeBlog("fishblogs", "bigcat", image, buffer);

All parts of the primary key need to be the same between the the different mutations.  Then, after you perform both mutations, you'll get a row back in CQL that looks like:

cqlsh:examples> select * from fishblogs;
 userid | when                     | fishtype | blog            | image
--------+--------------------------+----------+-----------------+----------------------
 bigcat | 2012-10-08 12:08:10-0400 |  CATFISH | this is myblog. | 01000000000000000000

Hopefully this clears things up for people.

8 comments:

Jonathan Ellis said...

Can you clarify why you need two writes here? I assume that with a non-CQL row Astyanax is able to write multiple columns at once.

Brian O'Neill said...

Astyanax can write multiple columns within a single mutation. What I meant by "two writes", is that it looks like two different writes from the API/design perspective. You need to call putColumn() twice with the same composite key and two different values. If you extend this to 10-20 "value" columns, you'll need to call putColumn 10-20 times. With other ORM layers, the "value columns" aren't treated differently. Instead they're just additional member variables on the annotated object, with a single call to "put".

Brian O'Neill said...

For clarity, by "value columns" I mean non-primary-key columns. Right now, Astyanax only provides annotation support for the primary keys, which are part of the compound key. I didn't see anyway to include the non-primary-keys in the annotated object using the Astyanax API.

Hopefully, with the work Vivek/Kundera and others are doing, the Java APIs and CQL will come into alignment.

vivek said...

I am trying to build this within Kundera, but looks like thrift API is not fully compatible with CQL3.0 for composite/compound keys. looks like only possible way for me is to translate all such stuff in CQL queries!

vivek said...
This comment has been removed by the author.
vivek said...

Basic support added for composite/compound key in Kundera.Still work in progress for JPA query support. Have a look at:
https://github.com/impetus-opensource/Kundera/blob/trunk/kundera-cassandra/src/test/java/com/impetus/client/crud/compositeType/CompositeTypeTest.java

about it's usage.

Waya said...

Thanks for the article and sample project. Really help me get my head around working with composite columns using Astyanax.

Jyotirmaya Padhi said...

Hi Brian,
This blog is really very helpful.

But I have query. Say if one of the column is of double type. Then could we proceed with the code. Since I am getting a error that it is expecting a byte array.

Hence if in the example the column "blog" is of double type. How could we resolve it?


Please help me with this asap.