问题描述
我正在开发一个项目,我需要使用 Cassandra数据库
。我有一个示例程序,将数据填充到 Cassandra数据库
。我现在使用 Pelops客户
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.
现在我正在考虑制作一个对于
Cassandra数据库
的 > 使用
Singelton类
到我的 CassandraDAO
中的实例插入到Cassandra数据库,并从Cassandra数据库检索数据。
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.
下面是我到目前为止构建的Singleton类,它将连接到Cassandra数据库 -
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;
}
}
问题陈述:
我在上面的代码中几乎没有怀疑。
I have few doubts in my above code.
- 我应该在
setSeeds
方法在我上面的类吗?任何指针或示例都会有很大的帮助。 - 其次,我不知道这是否是正确的方法,因为我创建一个Singleton类?我想知道什么是最好的方法是使用pelops客户端管理集群连接。
- 还有,使用
addPool的最好方法是
方法在我上面的代码?我想,我在那里搞砸了一些东西吗?我在Pelops类
中继续看到不同的addPool
方法?
- 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.
在上面的Singleton类之后,准备好,我打算在我的 DAO
代码中使用上面的类,像这样 -
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 $ c>;
Mutator mutator = Pelops.createMutator(CassandraConnection.getInstance().getPoolName());
mutator.writeColumns(other data inside)
;
然后选择器也用于检索数据。
And then do the selector as well for retrieving the data.
只是FYI我正在使用 Cassandra 1.2.3
和 Scale 7 pelops客户端
。
Just FYI, I am working with Cassandra 1.2.3
and Scale 7 pelops client
.
任何帮助将不胜感激。提前感谢。
Any help will be appreciated. Thanks in advance.
更新代码:
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;
}
}
任何人都可以看看,让我知道我的一切正确吗?感谢您的帮助。
Can anyone take a look and let me know I got everything correctly? Thanks for the help.
推荐答案
种子节点是两个(或更多,但2是Cassandra文档的建议数)节点您的群集。在每个Cassandra节点配置文件(cassandra.yaml)中,存在该簇的种子节点的地址。假设您有5个节点的集群
Seeds nodes are two (or more, but 2 is the suggested number from Cassandra documentation) nodes of your cluster. In each cassandra-node configuration file (cassandra.yaml) there is the address of the seeds nodes for the cluster. Imagine you have cluster of 5 nodes
192.168.1.100
192.168.1.101
192.168.1.102
192.168.1.103
192.168.1.104
192.168.1.100192.168.1.101192.168.1.102192.168.1.103192.168.1.104
种子
192.168。在每个配置文件中将有
in each config file there will be, for instance
1.101
192.168.1.103
Seeds 192.168.1.101 192.168.1.103
对于这个集群,这两个地址是种子节点。启动时集群的每个节点将联系这2个节点并获取必要的信息。在你的例子中,你可以传递在配置中找到的地址或只是对群集的地址节点的配对
For this cluster these 2 addresses are the seed nodes. Each node of the cluster at the startup will contact these 2 nodes and get the necessary information. In your example you can pass the addresses found in the configuration or just couple of address nodes of the cluster
String[] nodes = new String[2];
nodes[1] = "192.168.1.101";
nodes[2] = "192.168.1.103";
2)Singleton是绝对不必要的,因为Pelops类只由静态元素构成。如果您的应用程序中有一个Init / Startup,那么只需声明与Cassandra的连接,它将在您的所有代码中可用。
2) The Singleton is absolutely unnecessary since the Pelops class is made only by static elements. If you have an Init/Startup in your application just declare there the connection to Cassandra and it will be available in all your code
3)没有正确的答案,正确的方式连接到群集取决于群集。
您可能需要设置您的自定义参数或由Pelops保留一个。在我的生产env(5个节点,RF = 3)我使用默认参数没有问题。
3) There is no correct answer, the right way to connect to a cluster depends on the cluster.You may need to set your custom parameters or leave the one by Pelops. In my production env (5 nodes, RF=3) I'm using default params without problems.
Ciao
这篇关于使用Pelops客户端连接到Cassandra数据库的高效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!