Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 559
Raduan Santos Avatar asked Feb 06 '12 22:02

Raduan Santos


People also ask

How do I insert data into a column in Cassandra?

Command 'Insert into' writes data in Cassandra columns in row form. It will store only those columns that are given by the user. You have to necessarily specify just the primary key column. It will not take any space for not given values.

What is upsert and update in Cassandra?

Upsert means that Cassandra will insert a row if a primary key does not exist already otherwise if primary key already exists, it will update that row. The Cassandra Update query is used to update the data in the Cassandra table.

What happens when you delete a row in Cassandra?

When data is deleted, it is not deleted from the table immediately. Instead deleted data is marked with a tombstone and are removed after compaction. The above Cassandra delete row syntax will delete one or more rows depend upon data filtration in where clause.

What is the use of update query in Cassandra?

The Cassandra Update query is used to update the data in the Cassandra table. If no results are returned after updating data, it means data is successfully updated otherwise an error will be returned.


1 Answers

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

like image 110
nickmbailey Avatar answered Oct 04 '22 20:10

nickmbailey