java - 使用 Pelops 客户端连接到 Cassandra 数据库的有效方法

标签 java singleton cassandra pelops

我正在从事一个需要使用 Cassandra 数据库 的项目。我有一个将数据填充到 Cassandra 数据库 中的示例程序。为此,我正在使用 Pelops 客户端

所以现在我正在考虑为 Cassandra 数据库 创建一个 Singleton 类,它将连接到 Cassandra 数据库 然后我正在使用Singelton 类 中的那个实例到我的 CassandraDAO 中以插入 Cassandra 数据库并从 Cassandra 数据库中检索数据。

下面是我到目前为止构建的单例类,它将连接到 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;
    }
}

问题陈述:-

我对上面的代码几乎没有怀疑。

  1. 首先,我应该在上面的类的 setSeeds 方法中做什么?任何指示或示例都会有很大帮助。
  2. 其次,我不确定在创建 Singleton 类时这样做是否正确?我想知道管理与 pelops 客户端的集群连接的最佳方法是什么。
  3. 另外,在我上面的代码中使用 addPool 方法的最佳方式是什么?我想,我也搞砸了那边的东西?当我不断在 Pelops 类 中看到不同的 addPool 方法时?所以我应该牢记使用哪种方法,因为我将在生产环境中运行它。

在上面的 Singleton 类准备好之后,我打算在我的 DAO 代码中使用上面的类,就像这样-

Mutator mutator = Pelops.createMutator(CassandraConnection.getInstance().getPoolName()); mutator.writeColumns(里面的其他数据);

然后还执行选择器以检索数据。

仅供引用,我正在使用 Cassandra 1.2.3Scale 7 pelops 客户端

任何帮助将不胜感激。提前致谢。

更新代码:-

下面是我更新的代码。

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 是 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

在每个配置文件中都会有,例如<​​/p>

种子 192.168.1.101 192.168.1.103

对于这个集群,这两个地址是种子节点。集群的每个节点在启动时都会联系这 2 个节点并获取必要的信息。在您的示例中,您可以传递配置中找到的地址或集群的几个地址节点

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。在我的生产环境(5 个节点,RF=3)中,我使用默认参数没有问题。

再见

关于java - 使用 Pelops 客户端连接到 Cassandra 数据库的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15912501/

相关文章:

c# - 为多个接口(interface)返回相同的实例

java - 创建单例时使用锁定对象

c# - "Greater than"where-condition on timeuuid 使用 Datastax C# Cassandra 驱动程序

java - 我们可以在 Cassandra 数据库中设置用户定义的字段长度吗

java - 两个字符串均从数据库中获取时的 JSTL 字符串比较

java - 当代码具有注释和泛型时,maven 3 + pmd 失败

java - 有没有办法动态重启循环打印输出的 HelloWorld Java 程序?

java - Apache NiFi 自定义 NAR 启动期间出现 NoClassDefFoundError

Cassandra 如何查看事件用户连接

c# - 当某些内容缓存在 c# asp.net 中时,它是缓存在服务器上还是客户端上?