Click here to edit subtitle


Post Reply
Forum Home > Bigdata Learnings( Hadoop, HBase,Hive and other bigdata technologies) > Cassandra:Write data into table using Hector API

Sourav Gulati
Site Owner
Posts: 83

Lets take an example, we have a following file :

$ cat test

row-1 colfam1 quan1 val1

row-2 colfam1 quan2 val2


 Now , we have a table(columnfamily) in Cassandra "hbtable" in "mykeyspace' ,in which we have to load the data from this file . We have four columns in this table  (rowkey , cf , quantifier,value) with data type varchar .


Following is example of loading data in to this table using Cassandra Hector API:


public class SimpleWriteHector {

    public static void main(String[] args) throws IOException {

        Cluster cluster = HFactory.getOrCreateCluster("Test Cluster",",;9160");

        Keyspace keyspace = HFactory.createKeyspace("mykeyspace", cluster);

        Serializer se= StringSerializer.get() ;

        Mutator mutator = HFactory.createMutator(keyspace, se);

        BufferedReader br = new BufferedReader(new FileReader("/home/user/test"));

        String line;



            String columns[]=line.split("\\s");

             mutator.addInsertion(columns[0], "hbtable", HFactory.createStringColumn("cf",columns[1]))

             .addInsertion(columns[0], "hbtable", HFactory.createStringColumn("quantifier",columns[2]))

             .addInsertion(columns[0], "hbtable", HFactory.createStringColumn("value",columns[3]));




        System.out.println("Records inserted");




Lets check it now in Cassandra :


cqlsh:mykeyspace>select * from hbtable;

rowkey | cf | quantifier | value


row-2 | colfam1 | quan2 | val2

row-1 | colfam1 | quan1 | val1


Click here for Other topics of BigData Technologies



Tags: Load Data into Cassandra table

          Insert Data into Cassandra table



March 9, 2013 at 12:43 PM Flag Quote & Reply

You must login to post.