Skip to content

Data Types and Assume

edwardcapriolo edited this page May 3, 2013 · 3 revisions

Originally Cassandra was very schema-less. Over time more optional validation and and typing has been added to Cassandra. Cql on the far extreme is type heavy.

Writing data

A user can send data to IntraVert through the SET or BATCH verbs. This data can be either specified as a Byte[]/ByteBuffer. In this case the byte buffer is passed directly to Cassandra (Storage Proxy). If the data is malformed upstream should throw an exception which will be returned to the client.

It is also possible to send standard objects in Json like String and Integer. By default IntraVert will use the class of the object do the conversion and submit the proper byte buffer upstream.

Note: in the near future IntraVert may have to use schema because there are many Integer types i32, big integer, long, etc.

Reading data

In the near future IntraVert will use information in the response to type objects before returning them to the client. For now users have to use ASSUME.

Assume Verb

Assume allows us to override what Cassandra thinks a type is. This is most useful when Cassandra has no schema for a column but you as an end user know exactly what type it is.

req.add( Operations.assumeOp("compks", "compcf", "value", "CompositeType(UTF-8,int32)"));
req.add( Operations.assumeOp("compks", "compcf", "column", "int32"));//6

When a get or slice operation runs the value is converted on the server side and stored in the result.

req.add( Operations.getOp("rowa", 1)); //8
... send request
Assert.assertEquals( 1,  x.get(0).get("name") );
Assert.assertEquals("yo",  ((Object [])x.get(0).get("value"))[0] );
Assert.assertEquals( 2,  ((Object [])x.get(0).get("value"))[1] );

Ranged Assume

Some times a user divides a column family in such a way that columns a-b are ints and columns c-d are strings. Ranged assume is useful for this case.

@Test
@RequiresColumnFamily(ksName = "myks", cfName = "columnasscf")
public void rangedAssumeTest() throws Exception{
  IntraReq r = new IntraReq();
  r.add( Operations.assumeRangedOp("myks", "columnasscf", "e", "f", "Int32Type") )
   .add( Operations.assumeRangedOp("myks", "columnasscf", "y", "z", "UTF8Type") )
   .add( Operations.setKeyspaceOp("myks"))
   .add( Operations.setColumnFamilyOp("columnasscf"))
   .add( Operations.setOp("darow", "e", 10) )
   .add( Operations.setOp("darow", "e1", 11) )
   .add( Operations.setOp("darow", "e5", 9) )
   .add( Operations.setOp("darow", "y", "nice") )
   .add( Operations.setOp("darow", "y1", "dude") )
   .add( Operations.sliceOp("darow", "e", "z", 10));
  IntraClient2 ic2 = new IntraClient2("localhost", 8080);
  IntraRes res = ic2.sendBlocking(r);
  List<Map> x = (List<Map>) res.getOpsRes().get(9);
  Assert.assertEquals(10, x.get(0).get("value"));
  Assert.assertEquals(11, x.get(1).get("value"));
  Assert.assertEquals("nice", x.get(3).get("value"));
}