I am working on a project in which I need to use Cassandra Database
. I have a sample program that will populate data into Cassandra database
. I am using Pelops client
for that.
So now I am thinking of making a Singleton class
for Cassandra database
that will make a connection to Cassandra database
and then I be using that instance from Singelton class
into my CassandraDAO
to insert into Cassandra database and retrieve the data from Cassandra database as well.
Below is my Singleton class that I have built so far which will make a connection to Cassandra database-
public class CassandraConnection {
private static CassandraConnection _instance;
private String keyspace;
private String[] seeds;
private int port;
private String poolName;
public static synchronized CassandraConnection getInstance() {
if (_instance == null) {
_instance = new CassandraConnection();
}
return _instance;
}
private CassandraConnection() {
setKeyspace(ICassandraDo.KEYSPACE_NAME);
setSeeds(ICassandraDo.NODES).split(",");
setPort(ICassandraDo.CASSANDRA_PORT);
setPoolName(ICassandraDo.THRIFT_CONNECTION_POOL);
createPool();
}
//This is the right way to `addPool` in pelops?
private void createPool() {
Pelops.addPool(getPoolName(), getSeeds(), getPort(),
false, getKeyspace(), new Policy());
}
private String setSeeds(String nodes) {
// I am not sure what I am supposed to do here?
// Any guidance will be of great help
}
private void setPoolName(String thriftConnectionPool) {
this.poolName = thriftConnectionPool;
}
private void setPort(int cassandraPort) {
this.port = cassandraPort;
}
private void setKeyspace(String keyspaceName) {
this.keyspace = keyspaceName;
}
public void setSeeds(String[] seeds) {
this.seeds = seeds;
}
public String[] getSeeds() {
return seeds;
}
public int getPort() {
return port;
}
public String getKeyspace() {
return keyspace;
}
public String getPoolName() {
return poolName;
}
}
Problem Statement:-
I have few doubts in my above code.
- Firstly, what I am supposed to do in
setSeeds
method in my above class? Any pointers or example will be of great help. - Secondly, I am not sure whether this is the right way to do this as I am creating a Singleton class? I am wondering what's the best approach is for managing a cluster connection with pelops client.
- And also, what's the best way of using
addPool
method in my above code? I guess, I messed up something over there as well? As I keep on seeing differentaddPool
methods inPelops class
? So which method I should be using keeping in mind as I will be running this in Production environment.
And after the above Singleton class is ready, I am planning to use the above class in my DAO
code, something like this-
Mutator mutator = Pelops.createMutator(CassandraConnection.getInstance().getPoolName());
mutator.writeColumns(other data inside)
;
And then do the selector as well for retrieving the data.
Just FYI, I am working with Cassandra 1.2.3
and Scale 7 pelops client
.
Any help will be appreciated. Thanks in advance.
Updated Code:-
Below is my updated code.
public class CassandraConnection {
private static CassandraConnection _instance;
private String keyspace;
private String[] nodes;
private int port;
private String poolName;
public static synchronized CassandraConnection getInstance() {
if (_instance == null) {
_instance = new CassandraConnection();
}
return _instance;
}
private CassandraConnection() {
setKeyspace(ICassandraDo.KEYSPACE_NAME);
setNodes(ICassandraDo.NODES);
setPort(ICassandraDo.CASSANDRA_PORT);
setPoolName(ICassandraDo.THRIFT_CONNECTION_POOL);
createPool();
}
private void createPool() {
Pelops.addPool(getPoolName(), getCluster(), getKeyspace());
}
private Cluster getCluster() {
Config casconf = new Config(ICassandraDo.CASSANDRA_PORT, true, 0);
Cluster cluster= new Cluster(nodes, casconf, ICassandraDo.NODE_DISCOVERY);
return cluster;
}
private void setPoolName(String thriftConnectionPool) {
this.poolName = thriftConnectionPool;
}
private void setPort(int cassandraPort) {
this.port = cassandraPort;
}
private void setKeyspace(String keyspaceName) {
this.keyspace = keyspaceName;
}
private void setNodes(String nodes) {
this.nodes = nodes.split(",");
}
public int getPort() {
return port;
}
public String getKeyspace() {
return keyspace;
}
public String getPoolName() {
return poolName;
}
}
Just FYI, In my case, I am going to have two clusters each with 12 nodes.
Can anyone take a look and let me know I got everything correctly? Thanks for the help.