【发布时间】:2013-04-01 12:09:23
【问题描述】:
我正在做一个需要使用Cassandra Database 的项目。我有一个将数据填充到Cassandra database 的示例程序。我为此使用Pelops client。
所以现在我正在考虑为Cassandra database 建立一个Singleton class,它将与Cassandra database 建立连接,然后我将使用Singelton class 中的该实例插入到我的CassandraDAO 中以插入Cassandra 数据库并检索Cassandra 数据库中的数据也是如此。
下面是我迄今为止构建的 Singleton 类,它将连接到 Cassandra 数据库-
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;
}
}
问题陈述:-
我对上面的代码没有什么疑问。
- 首先,我应该在上面的课程中使用
setSeeds方法做什么?任何指示或示例都会有很大帮助。 - 其次,我不确定这是否是正确的方法,因为我正在创建一个 Singleton 类?我想知道管理与 pelops 客户端的集群连接的最佳方法是什么。
- 另外,在上面的代码中使用
addPool方法的最佳方式是什么?我想,我也搞砸了那里的东西?当我不断在Pelops class中看到不同的addPool方法时?所以我应该记住应该使用哪种方法,因为我将在生产环境中运行它。
在上面的Singleton类准备好之后,我打算在我的DAO代码中使用上面的类,像这样-
Mutator mutator = Pelops.createMutator(CassandraConnection.getInstance().getPoolName());
mutator.writeColumns(other data inside);
然后也执行选择器以检索数据。
仅供参考,我正在与Cassandra 1.2.3 和Scale 7 pelops client 合作。
任何帮助将不胜感激。提前致谢。
更新代码:-
下面是我更新的代码。
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;
}
}
仅供参考,就我而言,我将有两个集群,每个集群有 12 个节点。
任何人都可以看看,让我知道我得到了正确的一切吗?感谢您的帮助。
【问题讨论】:
标签: java singleton cassandra pelops