【问题标题】:Efficient way to connect to Cassandra database using Pelops client使用 Pelops 客户端连接到 Cassandra 数据库的有效方法
【发布时间】: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;
    }
}

问题陈述:-

我对上面的代码没有什么疑问。

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

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

Mutator mutator = Pelops.createMutator(CassandraConnection.getInstance().getPoolName()); mutator.writeColumns(other data inside);

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

仅供参考,我正在与Cassandra 1.2.3Scale 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


    【解决方案1】:

    种子节点是集群的两个(或更多,但 Cassandra 文档中建议的数量为 2)节点。在每个 cassandra-node 配置文件 (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

    对于这个集群,这两个地址是种子节点。集群的每个节点在启动时都会联系这 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)中,我使用默认参数没有问题。

    【讨论】:

    • 感谢卡洛的建议。有些事情对我来说很有意义。首先,在我当前的示例中,我应该在setSeeds 方法中添加哪些行?我相信我们需要在那里添加一条集群线?正确的?你能提供一个例子,我应该在那里添加什么,以便更好地了解这一点。其次,看看我的createPool method,我相信我在那里搞砸了。我找不到任何带有我签名的 addPool 方法。您能否为我提供一个示例以及您在生产环境中使用的任何内容,以便更好地理解。
    • 我用最新的代码更新了我的问题,你能看一下,让我知道一切是否正常吗?
    • 您将 setseeds 更改为 setnodes:正确。在我的 prod-env 中,我有一个集群,但从开发的角度来看,应该没有任何改变。这是我用来连接String[] nodes = cfg.getStringArray("cassandra.servers"); int port = cfg.getInt("cassandra.port"); boolean dynamicND = true; // dynamic node discovery Config casconf = new Config(port, true, 0); Cluster cluster = new Cluster(nodes, casconf, dynamicND); Pelops.addPool(Const.CASSANDRA_POOL, cluster, Const.CASSANDRA_KS); 的代码,如果您有两个集群,我认为最好的方法是将 4 个节点设置为 Pelops,每个集群设置 2 个。 HTH,卡洛
    • 谢谢卡洛。我将有两个集群,每个集群有 12 个节点。你说,我应该为 Pelops 设置 4 个节点,每个集群两个。正确的?您基于什么决定我应该为 Pelops 设置 4 个节点?我的印象是,如果我有 24 个节点,那么我应该在 pelops 中添加所有 24 个节点吗?这不是真的吗?如果我的理解有误,请纠正我?
    • 有什么想法吗?我正在学习过程中,所以任何指导都会有很大帮助。
    猜你喜欢
    • 2013-03-29
    • 2013-04-27
    • 2012-06-04
    • 1970-01-01
    • 2015-05-10
    • 2020-10-07
    • 2020-03-22
    • 2013-05-21
    • 1970-01-01
    相关资源
    最近更新 更多