Created
February 15, 2012 21:04
-
-
Save emaxerrno/1838998 to your computer and use it in GitHub Desktop.
Exception: TimeUUID should be 16 or 0 bytes (38)
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
/* | |
CQL CODE: | |
---------- | |
CREATE KEYSPACE TestSpace WITH strategy_class = 'NetworkTopologyStrategy'; | |
USE TestSpace; | |
CREATE COLUMNFAMILY TestColumnFamily ( | |
KEY 'TimeUUIDType' PRIMARY KEY | |
) WITH | |
comment='' AND | |
comparator='CompositeType(org.apache.cassandra.db.marshal.TimeUUIDType,org.apache.cassandra.db.marshal.UTF8Type)' AND | |
row_cache_provider='SerializingCacheProvider' AND | |
key_cache_size=200000.000000 AND | |
row_cache_size=0.000000 AND | |
read_repair_chance=1.000000 AND | |
gc_grace_seconds=864000 AND | |
default_validation=text AND | |
min_compaction_threshold=4 AND | |
max_compaction_threshold=32 AND | |
row_cache_save_period_in_seconds=0 AND | |
key_cache_save_period_in_seconds=14400 AND | |
replication_on_write=True; | |
*/ | |
import java.util.Arrays; | |
import me.prettyprint.cassandra.serializers.CompositeSerializer; | |
import me.prettyprint.cassandra.serializers.StringSerializer; | |
import me.prettyprint.cassandra.service.ThriftKsDef; | |
import me.prettyprint.cassandra.service.template.ColumnFamilyTemplate; | |
import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater; | |
import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate; | |
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.exceptions.HectorException; | |
import me.prettyprint.hector.api.factory.HFactory; | |
public class HectorTest { | |
public static String keyspaceName = "TestSpace", | |
columnFamilyName = "TestColumnFamily"; | |
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.COMPOSITETYPE); | |
// | |
// http://groups.google.com/group/hector-users/browse_thread/thread/ffd0895a17c7b43e) | |
cfd.setComparatorTypeAlias("(TimeUUIDType, UTF8Type)"); | |
cfd.setKeyValidationClass(ComparatorType.TIMEUUIDTYPE | |
.getClassName()); | |
cfd.setDefaultValidationClass(ComparatorType.UTF8TYPE | |
.getClassName()); | |
//TODO: update later | |
KeyspaceDefinition myKs = HFactory.createKeyspaceDefinition( | |
keyspaceName, ThriftKsDef.NETWORK_TOPOLOGY_STRATEGY, 1, | |
Arrays.asList(cfd)); | |
// Step 3: Add schema to the cluster | |
cluster.addKeyspace(myKs, true); | |
} | |
Keyspace ksp = HFactory.createKeyspace(keyspaceName, cluster); | |
ColumnFamilyTemplate<Composite, String> template = new ThriftColumnFamilyTemplate<Composite, String>( | |
ksp, columnFamilyName, CompositeSerializer.get(),StringSerializer.get()); | |
Composite c = new Composite(); | |
//Key for each row in Column Family as TimeUUIDType, UTF8Type | |
c.add(0,TimeUUIDUtils.getUniqueTimeUUIDinMillis()); | |
c.add(1,"[email protected]"); | |
ColumnFamilyUpdater<Composite, String> updater = template.createUpdater(c); | |
updater.setUUID("CompositeValue", TimeUUIDUtils.getUniqueTimeUUIDinMillis()); | |
try { | |
template.update(updater); | |
} catch (HectorException e) { | |
e.printStackTrace(); | |
// do something ... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment