Created
February 27, 2012 23:37
-
-
Save emaxerrno/1927928 to your computer and use it in GitHub Desktop.
Exception Thrown with this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import me.prettyprint.cassandra.model.HColumnImpl; | |
import me.prettyprint.cassandra.serializers.BytesArraySerializer; | |
import me.prettyprint.cassandra.serializers.DynamicCompositeSerializer; | |
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.DynamicComposite; | |
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 HecorTestDynamicCompositeColumn { | |
public static String keyspaceName = "ApplicationSpace18", | |
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.DYNAMICCOMPOSITETYPE); //for column keys! | |
cfd.setComparatorTypeAlias("(t=>TimeUUIDType,i=> IntegerType)"); //column keys | |
cfd.setKeyValidationClass("BytesType"); // 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); | |
//The mutator is a mutator for ANY column family. | |
Mutator<byte[]> mutator = HFactory.createMutator(ksp, BytesArraySerializer.get()); | |
//The columns inside the "composite row" are simply DynamicComposite:String for key:value pairs. | |
//You can exapnd this model and use Composite:String or whatever other time you want | |
HColumnImpl<DynamicComposite, String> column = new HColumnImpl<DynamicComposite, String>(DynamicCompositeSerializer.get(), StringSerializer.get()); | |
column.setClock(ksp.createClock()); | |
// family above. | |
DynamicComposite colKey = new DynamicComposite(); | |
colKey.add(0, TimeUUIDUtils.getUniqueTimeUUIDinMillis()); | |
colKey.add(1, 23); | |
column.setName(colKey); | |
column.setValue("[email protected]"); | |
//insert on RowKey: 49760920-58db-11e1-be0d-3c07546ac0c4:userIdentifyierMD5Hash | |
mutator.insert("realByteArray".getBytes(), columnFamilyName, column); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment