Skip to content

Instantly share code, notes, and snippets.

@emaxerrno
Created February 16, 2012 20:19
Show Gist options
  • Save emaxerrno/1847558 to your computer and use it in GitHub Desktop.
Save emaxerrno/1847558 to your computer and use it in GitHub Desktop.
Working Example of Composite Column
/*Date: February 16, 2012
*Author: Alex Gallego
*/
import java.util.Arrays;
import me.prettyprint.cassandra.model.HColumnImpl;
import me.prettyprint.cassandra.serializers.CompositeSerializer;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.utils.TimeUUIDUtils;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.Composite;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
public class HectorTest {
public static String keyspaceName = "ApplicationSpace3",
columnFamilyName = "TheFamily";
public static void main(String... args) {
// Step 1: Create a cluster
Cluster cluster = HFactory.getOrCreateCluster("Test Cluster",
"192.168.2.38:9160");
KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(keyspaceName);
if (keyspaceDef == null) {
// Step 2: Create the schema
ColumnFamilyDefinition cfd = HFactory.createColumnFamilyDefinition(
keyspaceName, columnFamilyName,
ComparatorType.UTF8TYPE); //for column keys!
cfd.setKeyValidationClass("CompositeType(TimeUUIDType,UTF8Type)"); // for row keys
cfd.setDefaultValidationClass(ComparatorType.UTF8TYPE // column values, not keys
.getClassName());
KeyspaceDefinition myKs = HFactory.createKeyspaceDefinition(
keyspaceName, "SimpleStrategy", 2,
Arrays.asList(cfd));
// Step 3: Add schema to the cluster and waits for agreement from cluster
cluster.addKeyspace(myKs, true);
}
Keyspace ksp = HFactory.createKeyspace(keyspaceName, cluster);
//Here is the tricky part:
//The mutator is a mutator for ANY column family. However see below how we use it to insert
// on `columnFamilyName`
Mutator<Composite> mutator = HFactory.createMutator(ksp, CompositeSerializer.get());
//The columns inside the "composite row" are simply String:String for key:value pairs.
//You can exapnd this model and use Composite:String or whatever other time you want
HColumnImpl<String, String> column = new HColumnImpl<String, String>(StringSerializer.get(), StringSerializer.get());
column.setClock(ksp.createClock());
//This MUST contain TimeUUIDType:UTF8Type as declared when we created the column
// family above.
Composite rowKey = new Composite();
rowKey.add(0, TimeUUIDUtils.getUniqueTimeUUIDinMillis());
rowKey.add(1, "userIdentifyierMD5Hash");
column.setName("email");
column.setValue("email@example.com");
//insert on RowKey: 49760920-58db-11e1-be0d-3c07546ac0c4:userIdentifyierMD5Hash
mutator.insert(rowKey, columnFamilyName, column);
//user the same column to keep adding values to the row
column.setName("Subject");
column.setValue("About That code review");
//insert on RowKey: 49760920-58db-11e1-be0d-3c07546ac0c4:userIdentifyierMD5Hash
mutator.insert(rowKey, columnFamilyName, column);
//This will produce something like this:
//-------------------
//RowKey: 49760920-58db-11e1-be0d-3c07546ac0c4:userIdentifyierMD5Hash
//=> (column=Subject, value=About That code review, timestamp=1329423465117000)
//=> (column=email, value=email@example.com, timestamp=1329423465117000)
}
}
@gireeshp
Copy link

CompositeSerializer.get() is not working. Which api version supports this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment