javanosqlcassandrahector

insert row on keyspace in Apache Cassandra + Hector + Java


I'm studying the Apache Cassandra version 0.7.6 with Java and Hector, and I tried to create a cluster, a keyspace and insert a column in this keyspace created.

By looking examples I understood that keyspace is equivalent to the database in SQL databases, and the Column Family's is equivalent with the tables. Knowing this I tried to create my simple example structure.

 Cluster tutorialCluster = HFactory.getOrCreateCluster("TutorialCluster","127.0.0.1:9160");
 ConfigurableConsistencyLevel ccl = new ConfigurableConsistencyLevel();

 ccl.setDefaultReadConsistencyLevel(HConsistencyLevel.ONE);

 Keyspace tutorialKeyspace = HFactory.createKeyspace("Tutorial", tutorialCluster, ccl);
 Mutator<String> mutator = HFactory.createMutator(tutorialKeyspace, stringSerializer);

 mutator.addInsertion("CA Burlingame", "StateCity", HFactory.createColumn(650L, "37.57x122.34", longSerializer, stringSerializer));

 MutationResult mr = mutator.execute();

But when I tried to run this with the cassandra started, but it returns an exception.

Exception in thread "main" me.prettyprint.hector.api.exceptions.HInvalidRequestException: InvalidRequestException(why:Keyspace Tutorial does not exist)
at me.prettyprint.cassandra.connection.HThriftClient.getCassandra(HThriftClient.java:70)
at me.prettyprint.cassandra.connection.HConnectionManager.operateWithFailover(HConnectionManager.java:226)

But I already created the "Tutorial" keyspace, and used in the mutator.


Solution

  • The createKeyspace() call in HFactory is meant for creating a hector Keyspace object for local use, but it does not actually create a keyspace in Cassandra. For that you want to use the 'addKeyspace()' and 'addColumnFamily' methods on the actual cluster object.

    https://github.com/rantav/hector/blob/master/core/src/main/java/me/prettyprint/hector/api/Cluster.java#L117