SuperColumns, VirtualKeyspaces, and Exceptions...oh my

41 views
Skip to first unread message

Steven Siebert

unread,
Jun 21, 2012, 3:11:51 PM6/21/12
to hector...@googlegroups.com
Hello all,

I'm new to Cassandra/Hector but have been reading the available docs and Hector unit tests/examples for the last 24hrs and can't seem to get what I need done correctly.  The end state is that I need to programmatically create my VirtualKeyspace(s) and populate it with a CF with a SuperColumn.  Doing a similar thing for a non-virtual keyspace with standard CF was easy - this is proving to be a lot tricker.

Ultimately, I believe I want my virtual data model to look something like this:
{
   "vkeyspace": {  //virtual keyspace prefixed by item "type"
      "DataItems": {   //column family
         "item1":{   //DataItem instance identifier
           "data1": {"some" : "data","something": "else"},
           "data2":{ "another": "dataset"}
          },
          "item2": { //another DataItem instance
             .....
          }
       }
    }
}

My POJO can be boiled down to look like this:
public class DataItem {
   private String key;
   private String type;  //this is the virtual keyspace prefix
   private Map<String, Map<String, String>> dataContainer; //outer key is supercolumn key, named above as "data#"
}

As I state in my comment above, the DataItem#type will be used as the virtual namespace prefix.  

I need to programmatically create these namespaces....so here is what I have to insert:

    public static void main(String[] args) throws Exception {
    GimsSingleDataItem si = new GimsSingleDataItem();
        String crId = UUID.randomUUID().toString();
        si.setKey(crId);
        si.setType("cr");
        si.getData().put("some", "data");
        si.getData().put("something", "else");


        final String PROPERTY_SOCKET = "localhost:9160";
        final String PROPERTY_CLUSTER_NAME =  "testCluster";
        final String PROPERTY_KEYSPACE =  "virtualKeyspaceTest";
        final String PROPERTY_COLUMNFAMILY =  "testitemCF";
        final int PROPERTY_REPLICATION_FACTOR =  1;

        Cluster cluster = HFactory.getOrCreateCluster(PROPERTY_CLUSTER_NAME,
                PROPERTY_SOCKET);


        KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(PROPERTY_KEYSPACE);

        if (keyspaceDef == null) {
            
            ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(PROPERTY_KEYSPACE,
                    PROPERTY_COLUMNFAMILY, ComparatorType.BYTESTYPE);
            cfDef.setColumnType(ColumnType.SUPER);

            keyspaceDef = HFactory.createKeyspaceDefinition(PROPERTY_KEYSPACE,
                    ThriftKsDef.DEF_STRATEGY_CLASS,
                    Integer.valueOf(PROPERTY_REPLICATION_FACTOR),
                    Arrays.asList(cfDef));
            cluster.addKeyspace(keyspaceDef, true);

        }

        Keyspace ks = HFactory.createVirtualKeyspace(PROPERTY_KEYSPACE,
                si.getType(), StringSerializer.get(), cluster);

        Mutator<String> mutator = HFactory.createMutator(ks, StringSerializer.get());
        mutator.insert(si.getKey(), PROPERTY_COLUMNFAMILY,
                HFactory.createSuperColumn("data", createColumnsFromMap(si.getData()),
                StringSerializer.get(), StringSerializer.get(), StringSerializer.get()));
    }


    private static List<HColumn<String, String>> createColumnsFromMap(Map<String, String> data) {
        List<HColumn<String, String>> columns = new ArrayList<>();
        for (String key : (Set<String>) data.keySet()) {
            columns.add(HFactory.createStringColumn(key, data.get(key)));
        }
        return columns;
    }

But I get this exception:

Exception in thread "main" me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:unconfigured columnfamily testSuperCF)
    at me.prettyprint.cassandra.service.ExceptionsTranslatorImpl.translate(ExceptionsTranslatorImpl.java:45)
    at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:264)
    at me.prettyprint.cassandra.model.ExecutingVirtualKeyspace.doExecuteOperation(ExecutingVirtualKeyspace.java:66)
    at me.prettyprint.cassandra.model.MutatorImpl.execute(MutatorImpl.java:243)
    at me.prettyprint.cassandra.model.MutatorImpl.insert(MutatorImpl.java:77)
    at com.candlelightcomputing.test.cassandra.App.main(App.java:62)
Caused by: InvalidRequestException(why:unconfigured columnfamily testSuperCF)
    at org.apache.cassandra.thrift.Cassandra$batch_mutate_result.read(Cassandra.java:19479)
    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.thrift.AbstractThriftClientWrapper.batch_mutate(AbstractThriftClientWrapper.java:64)
    at me.prettyprint.cassandra.service.VirtualKeyspaceCassandraClient.batch_mutate(VirtualKeyspaceCassandraClient.java:105)
    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.service.VirtualKeyspaceOperation.executeAndSetResult(VirtualKeyspaceOperation.java:50)
    at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:258)
    ... 4 more

I'm kinda at a loss what I'm doing wrong.

Thanks!

S

Nate McCall

unread,
Jun 21, 2012, 3:55:37 PM6/21/12
to hector...@googlegroups.com
Perhaps this is just a transposition error, but in your code, you are
referencing "testitemCF" as the name of the column family, but that
exception you posted indicates something is looking for "testSuperCF"
which is not referenced in the block of code you posted.

Steven Siebert

unread,
Jun 21, 2012, 4:01:09 PM6/21/12
to hector...@googlegroups.com
Ah, you got me!  I noticed that as soon as I hit send.   You're right, it was simply a transposition error =)

S
Reply all
Reply to author
Forward
0 new messages