を使用する必要があるプロジェクトに取り組んでいますCassandra Database
。にデータを入力するサンプル プログラムがありますCassandra database
。そのために使っPelops client
ています。
そのため、接続を確立するSingleton class
forを作成し、そのインスタンスを使用して Cassandra データベースに挿入し、Cassandra データベースからデータを取得することを考えています。Cassandra database
Cassandra database
Singelton class
CassandraDAO
以下は、これまでに作成したシングルトン クラスで、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 クラスを作成しているので、これが正しい方法かどうかわかりません。ペロプスクライアントとのクラスター接続を管理するための最良のアプローチは何だろうと思っています。
- また
addPool
、上記のコードでメソッドを使用する最良の方法は何ですか? あそこも何か間違えたのかな?addPool
でさまざまなメソッドを見続けているので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;
}
}
参考までに、私の場合、それぞれ 12 ノードの 2 つのクラスターを作成します。
誰でも見て、私がすべて正しく得たことを知らせてもらえますか? 助けてくれてありがとう。