Example #1
0
  /**
   * Create a Cassandra Cluster instance given the connection details
   *
   * @param userName userName to connect to the cassandra
   * @param password password to connect to cassandra
   * @param clusterName Cluster name
   * @param connectionString cassandra connection string
   * @return Cluster instance
   * @throws CassandraDataAccessException In case of and error in accessing database or data error
   */
  public static Cluster createCluster(
      String userName, String password, String clusterName, String connectionString)
      throws CassandraDataAccessException {

    if (userName == null || password == null) {
      throw new CassandraDataAccessException(
          "Can't create cluster with empty userName or Password");
    }

    if (clusterName == null) {
      throw new CassandraDataAccessException("Can't create cluster with empty cluster name");
    }

    if (connectionString == null) {
      throw new CassandraDataAccessException("Can't create cluster with empty connection string");
    }

    Map<String, String> credentials = new HashMap<String, String>();
    credentials.put(USERNAME_KEY, userName);
    credentials.put(PASSWORD_KEY, password);

    CassandraHostConfigurator hostConfigurator = new CassandraHostConfigurator(connectionString);
    hostConfigurator.setMaxActive(2000);
    Cluster cluster = HFactory.getCluster(clusterName);

    if (cluster == null) {
      cluster = HFactory.createCluster(clusterName, hostConfigurator, credentials);
    }
    return cluster;
  }