我正在做一个需要使用Cassandra Database的项目。我有一个示例程序,可以将数据填充到Cassandra database中。为此,我使用了Pelops client。
所以现在我正在考虑为Singleton class做一个Cassandra database,它将连接到Cassandra database,然后我使用来自Singelton class的实例插入到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方法中做什么?任何指点或例子都会有很大帮助。addPool方法的最佳方法是什么?我想,我在那边也搞砸了什么?因为我一直在Pelops class中看到不同的Pelops class方法?所以,我应该使用哪种方法,请记住,因为我将在生产环境中运行这个方法。在上面的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;
}
}只是FYI,在我的例子中,我将有两个集群,每个集群有12个节点。
谁能看一看,让我知道一切都是正确的吗?谢谢你的帮助。
发布于 2013-04-09 21:49:44
种子节点是集群的两个节点(或更多,但2个是Cassandra文档中建议的数字)。在每个cassandra节点配置文件(cassandra.yaml)中,都有集群种子节点的地址。假设您的集群由5个节点组成
192.168.1.100 192.168.1.101 192.168.1.102 192.168.1.103 192.168.1.104
例如,在每个配置文件中都有
种子192.168.1.101 192.168.1.103
对于这个集群,这两个地址是种子节点。启动时集群的每个节点都将与这两个节点联系,并获得必要的信息。在您的示例中,您可以传递在配置中找到的地址,或者只传递集群的两个地址节点。
String[] nodes = new String[2];
nodes[1] = "192.168.1.101";
nodes[2] = "192.168.1.103";2) Singleton绝对没有必要,因为Pelops类仅由静态元素组成。如果您的应用程序中有Init/Startup,只需声明到Cassandra的连接,它将在您的所有代码中可用
3)没有正确的答案,连接到集群的正确方式取决于集群。您可能需要设置自定义参数,或者将参数保留在Pelops旁边。在我的生产env (5个节点,RF=3)中,我使用默认的params,没有问题。
再见
https://stackoverflow.com/questions/15912501
复制相似问题