-
-
Save philshon/1944629 to your computer and use it in GitHub Desktop.
Working full example of a working composite type for row keys.
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.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); | |
//cfd.setComparatorTypeAlias("(TimeUUIDType,UTF8Type)"); | |
cfd.setKeyValidationClass("CompositeType(TimeUUIDType,UTF8Type)"); // for row keys | |
cfd.setDefaultValidationClass(ComparatorType.UTF8TYPE | |
.getClassName()); | |
KeyspaceDefinition myKs = HFactory.createKeyspaceDefinition( | |
keyspaceName, "SimpleStrategy", 2, | |
Arrays.asList(cfd)); | |
// Step 3: Add schema to the cluster | |
cluster.addKeyspace(myKs, true); | |
} | |
Keyspace ksp = HFactory.createKeyspace(keyspaceName, cluster); | |
Mutator<Composite> mutator = HFactory.createMutator(ksp, CompositeSerializer.get()); | |
HColumnImpl<String, String> column = new HColumnImpl<String, String>(StringSerializer.get(), StringSerializer.get()); | |
column.setClock(ksp.createClock()); | |
Composite rowKey = new Composite(); | |
rowKey.add(0, TimeUUIDUtils.getUniqueTimeUUIDinMillis()); | |
rowKey.add(1, "[email protected]"); | |
column.setName("email"); | |
column.setValue("[email protected]"); | |
mutator.insert(rowKey, columnFamilyName, column); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment