Tuesday, September 25, 2012

Apache Cassandra Java Program(Hector)


Hector is a high level java client for apache cassandra.
If you want to know more features of it, click here.

I'll show you the java code for connecting to cassandra database.
The following codes for inserting and reading.

Main.java

import java.util.concurrent.LinkedBlockingQueue;

public class Main
{
    private static LinkedBlockingQueue queue = new LinkedBlockingQueue();
    private static int numThreads;

    public static void main(String[] args)
    {
        String host = "192.168.0.1"
        int port = 9160;
        //cluster number
        String cluster = "Test Cluster";
        //keyspace name
        String keySpace = "keyspace_demo";
        //column family
        String colFamily = "colFamily_demo";

        for(int i = 10; i <20 i="i">

        {
            queue.offer(i);
        }
           
        CassandraTest cassandraTest = new CassandraTest();    
         cassandraTest.run();        
    }
}




CassandraTest.java


import java.util.concurrent.LinkedBlockingQueue;
import me.prettyprint.cassandra.model.ConfigurableConsistencyLevel; import me.prettyprint.cassandra.serializers.IntegerSerializer; import me.prettyprint.cassandra.serializers.StringSerializer; import me.prettyprint.cassandra.service.CassandraHostConfigurator; import me.prettyprint.hector.api.Cluster; import me.prettyprint.hector.api.HConsistencyLevel; 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 CassandraTest  {     private final String host;     private final int port;     private final String cCluster;     private final String keySpace;     private final String mode;     private String colFamily;     private String columnName = "number";     private LinkedBlockingQueue queue;          private CassandraHostConfigurator cassandraHostConfigurator;     private Cluster cluster;     private Mutator mutator;     private ColumnQuery columnQuery;     private QueryResult> result;          public CassandraTest(String _host, int _port, String _cCluster, String _keySpace, String _colFamily, LinkedBlockingQueue _queue)     {         host = _host;         port = _port;         cCluster = _cCluster;         keySpace = _keySpace;         colFamily = _colFamily;         queue = _queue;           setup();     }     private void setup()     {        cassandraHostConfigurator = new CassandraHostConfigurator(host+":"+port);        cluster = HFactory.getOrCreateCluster(cCluster, cassandraHostConfigurator);         Keyspace kpo = HFactory.createKeyspace(keySpace, cluster);         mutator = HFactory.createMutator(kpo, IntegerSerializer.get());         columnQuery = HFactory.createColumnQuery(kpo, IntegerSerializer.get(), StringSerializer.get(), IntegerSerializer.get());     }      //read and write      public void run()     {            while(queue.isEmpty() == false)         {             int i = queue.poll();             try             {                 int numberValue = 0;                     // read from column family                     try                      {                         columnQuery.setColumnFamily(colFamily).setKey(i).setName(pcm);                         result = columnQuery.execute();                         // get the column value                         numberValue = result.get().getValue();                     }                      catch (Exception e)                      {                                        }                                               // write into column family                     mutator.insert(i, colFamily, HFactory.createColumn(columnName, (numberValue + 1)));             }             catch (HectorException e)             {                 System.out.println("HectorException-" + i + ": " + e.getMessage());             }         }     } }


No comments:

Post a Comment