Simplest way to insert data into a fresh Cassandra database using the Hector API?

user1258361 picture user1258361 · Apr 12, 2012 · Viewed 7.9k times · Source

I've followed numerous examples on inserting data into a Cassandra database and every time I get an exception about unconfigured column families.

Exception in thread "main" me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:unconfigured columnfamily TestColumnFamily)
    at me.prettyprint.cassandra.service.ExceptionsTranslatorImpl.translate(ExceptionsTranslatorImpl.java:45)
    at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:252)
    at me.prettyprint.cassandra.model.ExecutingKeyspace.doExecuteOperation(ExecutingKeyspace.java:97)
    at me.prettyprint.cassandra.model.MutatorImpl.execute(MutatorImpl.java:243)
    at me.prettyprint.cassandra.model.MutatorImpl.insert(MutatorImpl.java:69)
    at CassandraInterface.main(CassandraInterface.java:101)
Caused by: InvalidRequestException(why:unconfigured columnfamily TestColumnFamily)
    at org.apache.cassandra.thrift.Cassandra$batch_mutate_result.read(Cassandra.java:19477)
    at org.apache.cassandra.thrift.Cassandra$Client.recv_batch_mutate(Cassandra.java:1035)
    at org.apache.cassandra.thrift.Cassandra$Client.batch_mutate(Cassandra.java:1009)
    at me.prettyprint.cassandra.model.MutatorImpl$3.execute(MutatorImpl.java:246)
    at me.prettyprint.cassandra.model.MutatorImpl$3.execute(MutatorImpl.java:243)
    at me.prettyprint.cassandra.service.Operation.executeAndSetResult(Operation.java:103)
    at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:246)
    ... 4 more

So I looked up how to configure them and found

    BasicColumnFamilyDefinition cfdef = new BasicColumnFamilyDefinition();
    cfdef.setKeyspaceName(keyspaceName);
    cfdef.setName(columnFamilyName);
    cfdef.setKeyValidationClass(ComparatorType.UTF8TYPE.getClassName());
    cfdef.setComparatorType(ComparatorType.UTF8TYPE);

That didn't configure the column family.

All of the examples I have found are fragments without any context, so I don't know what to import or set up. In addition, some examples appear to mix the Hector API v2 and the original Hector API, so when I use them, I get "class not found" or "function not found" compiler errors.

Answer

sdolgy picture sdolgy · Apr 12, 2012

Hector CassandraClusterTest.java

 @Test
 public void testAddDropColumnFamily() throws Exception {
      ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("Keyspace1", "DynCf");
      cassandraCluster.addColumnFamily(cfDef);
      String cfid2 = cassandraCluster.dropColumnFamily("Keyspace1", "DynCf");
      assertNotNull(cfid2);

      // Let's wait for agreement
      cassandraCluster.addColumnFamily(cfDef, true);
      cfid2 = cassandraCluster.dropColumnFamily("Keyspace1", "DynCf", true);
      assertNotNull(cfid2);
 }

Long story short, keyspace and column family need to exist before you try and insert data into them. You can either manage this in your code, to check to see if they exist, using the example above as a nice reference -- or modify via the command line interface (cassandra-cli)

Hector Unit Tests