TECH SOLUTIONS

Click here to edit subtitle

Forums

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

Sourav Gulati
Site Owner
Posts: 83

Here is an example Cassandra Table(column Family) "hbtable" in "mykeyspace" :

 

cqlsh:mykeyspace>select * from hbtable;

rowkey | cf | quantifier | value

--------+---------+------------+-------

row-2 | colfam1 | quan2 | val2

row-1 | colfam1 | quan1 | val1


 Now lets read the data from this table using Hector API:


 public class SimpleReadHector {

    public static void main(String[] args) {

       

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

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

        Serializer se= StringSerializer.get() ;

// CqlQuery API lets you execute a cql query

        CqlQuery cqlQuery = new CqlQuery(keyspace, se, se,se);

        cqlQuery.setQuery("select * from hbtable");

        QueryResult> result = cqlQuery.execute();

              OrderedRows orderedRows = result.get(); 

       

        for (Row rrderedRows )

        {

            System.out.println(" "+r);

                   }      

       

// Seond way is to use RangeSlice API , where you can provide keys , ranges etc to read the values

        RangeSlicesQuery rangeSlicesQuery =

                HFactory.createRangeSlicesQuery(keyspace, se,se,se);

            rangeSlicesQuery.setColumnFamily("hbtable");           

            rangeSlicesQuery.setKeys("row-2", "");

              rangeSlicesQuery.setRange("", "", false, 4);

            rangeSlicesQuery.setRowCount(2); //if you need just top two rows to be scanned

            QueryResult> result1 = rangeSlicesQuery.execute();

            OrderedRows orderedRows = result1.get();           

                 

            System.out.println("Contents of rows: \n");                      

            for (Row r : orderedRows) {

                System.out.println("   " + r);

       

    }

   

}

}

 

 

 


Click here for Other topics of BigData Technologies

 

 

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

You must login to post.