Inserting and querying a single row with single column in Cassandra using Hector API


The Below example inserts a Column with the column name "Name" and the column value of "Deepak" under the key "RK1". The retrieving and instance of the Mutator class from HFactory makes this action straight-forward .

Retrieving a column from Cassandra is also straight forward operation. Using the ColumnQuery object to retrieve the information we inserted in the example. querying 


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.ColumnQuery;
import me.prettyprint.hector.api.query.QueryResult;


public class InsertSingleColumn {
    
private static StringSerializer stringSerializer = StringSerializer.get();
    
public static void main(String[] args) throws Exception {
Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "xxx.xxx.xxx.xxx:9160");

Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster);
try {
Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, StringSerializer.get());
mutator.insert("RK1", "TestCF", HFactory.createStringColumn("Name", "Deepak"));
/*Querying the inserted column*/            

ColumnQuery<String, String, String> columnQuery = HFactory.createStringColumnQuery(keyspaceOperator);
            columnQuery.setColumnFamily("TestCF").setKey("Name").setName("Deepak");
QueryResult<HColumn<String, String>> result = columnQuery.execute();
            
System.out.println("Read HColumn from cassandra: " + result.get());            
            
        } catch (HectorException e) {
            e.printStackTrace();
        }
        cluster.getConnectionManager().shutdown();
    }
    

}


Reference:GitHub


Find Comments below or Add one

Anonymous said...

Can you post the link from where one can download the hector API. there are some issues in downloading the same from github.

Deepak Kumar said...

Hi Anonymous
tats what i m going to call you.. since you have not written your name :D
So have you tried the link
https://github.com/rantav/hector
you will find the download link in the bottom right of the page. Coz this works for me.
And what are you using Apache Cassandra or Datastax. Better go for datastax for devlpoers, it is for free and bundled with lots of stuff.
there in datastax you can find the Hector API inside : dse-x.x.x/resources/cassandra/lib
note: here x.x.x is your datastax version.
Hope this helps you

Anonymous said...

I will try datastax for developers. For now , I was able to download the hector zip file from github and trying to use the same with Eclipse IDE. Any suggestions how to do that??

Deepak Kumar said...

Well just try making jar of the same. Import into eclipse and try it.

Deepak Kumar said...

Hey sorry it was my bad. You will not find the Hector jar inside dse-x.x.x/resources/cassandra/lib as i mentioned above... Actually i kept it for my use inside that folder so i thought it was there...
For the same you can go to
github.com/hector-client/hector/downloads
download the version you want. Extract it and inside that you will find your hector jar.

Thank you and i am really sorry for the wrong information :)

Anonymous said...

Can you be specific and let me know what version of hector works with which version of Cassandra?? I am trying to get this working but am getting various errors.

Deepak Kumar said...

@Anonymous: well i have tried with (hector-core-1.0-5.tgz and cassandra 1.1) and also with (hector-core-1.1-2.tgz with cassandra 2.0) and it worked for me...

Post a Comment