コード例 #1
0
 /** Common initializer. */
 private void init() {
   Metadata metadata = cluster.getMetadata();
   LOGGER.info("Connected to cluster: {}", metadata.getClusterName());
   for (Host h : metadata.getAllHosts()) {
     LOGGER.info(
         "Data center: {}; Hosts: {}; Rack: {}", h.getDatacenter(), h.getAddress(), h.getRack());
   }
 }
コード例 #2
0
 public void connect(String node) {
   cluster = Cluster.builder().addContactPoint(node).build();
   Metadata metadata = cluster.getMetadata();
   System.out.printf("Connected to cluster: %s\n", metadata.getClusterName());
   for (Host host : metadata.getAllHosts()) {
     System.out.printf(
         "Datatacenter: %s; Host: %s; Rack: %s\n",
         host.getDatacenter(), host.getAddress(), host.getRack());
   }
   session = cluster.connect("whoami");
 }
コード例 #3
0
  @Override
  public void start() {
    cluster = Cluster.builder().addContactPoint(address).build();
    Metadata metadata = cluster.getMetadata();
    LOGGER.debug("Connected to cluster: {0}", metadata.getClusterName());
    for (Host host : metadata.getAllHosts()) {
      LOGGER.debug(
          "Datacenter: {0}; Host: {1}; Rack: {2}",
          host.getDatacenter(), host.getAddress(), host.getRack());
    }

    session = cluster.connect();
    try {
      session.execute(
          "CREATE KEYSPACE modeshape WITH replication "
              + "= {'class':'SimpleStrategy', 'replication_factor':3};");
    } catch (AlreadyExistsException e) {
    }

    session.execute("USE modeshape;");

    try {
      session.execute(
          "CREATE TABLE modeshape.binary("
              + "cid text PRIMARY KEY,"
              + "mime_type text,"
              + "ext_text text,"
              + "usage int,"
              + "usage_time timestamp,"
              + "payload blob)");
    } catch (AlreadyExistsException e) {
    }

    try {
      session.execute("CREATE INDEX USAGE_IDX ON modeshape.binary (usage);");
    } catch (InvalidQueryException e) {
      // exists
    }

    try {
      session.execute("CREATE INDEX EXPIRE_IDX ON modeshape.binary (usage_time);");
    } catch (InvalidQueryException e) {
      // exists
    }
  }
  public static Cluster createCluster(CassandraConfiguration config) throws DataSourceException {

    boolean debugEnabled = false;
    if (log.isDebugEnabled()) {
      debugEnabled = true;
    }

    String userName = config.getUsername();
    String password = config.getPassword();
    List<String> connections = config.getHosts();
    String clusterName = config.getClusterName();
    int port = config.getPort();
    boolean jmxDisabled = config.getJmxDisabled();

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

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

    if (connections == null || connections.isEmpty()) {
      throw new DataSourceException("Can't create cluster with empty connection string");
    }

    int maxConnections = config.getMaxConnections();
    int concurrency = config.getConcurrency();
    boolean async = config.getAsync();
    String compression = config.getCompression();

    StringBuilder configProps = new StringBuilder();
    configProps
        .append("  concurrency:          " + concurrency)
        .append("\n  mode:                 " + (async ? "asynchronous" : "blocking"))
        .append("\n  per-host connections: " + maxConnections)
        .append("\n  compression:          " + compression);

    if (debugEnabled) {
      log.debug(configProps.toString());
    }

    Cluster cluster = null;

    try {
      PoolingOptions poolOptions = CassandraDatasourceUtils.createPoolingOptions(config);
      SocketOptions socketOptions = CassandraDatasourceUtils.createSocketOptions(config);

      // Create cluster

      Builder builder = Cluster.builder();

      for (String con : connections) {
        builder.addContactPoints(con);
      }
      builder
          .withPoolingOptions(poolOptions)
          .withSocketOptions(socketOptions)
          .withPort(port)
          .withCredentials(userName, password);

      CassandraDatasourceUtils.createRetryPolicy(config, builder);
      CassandraDatasourceUtils.createReconnectPolicy(config, builder);
      CassandraDatasourceUtils.createLoadBalancingPolicy(config, builder);
      if (jmxDisabled) {
        builder.withoutMetrics();
        builder.withoutJMXReporting();
      }

      if (ProtocolOptions.Compression.SNAPPY.name().equalsIgnoreCase(compression)) {
        builder.withCompression(ProtocolOptions.Compression.SNAPPY);
      } else {
        builder.withCompression(ProtocolOptions.Compression.NONE);
      }

      cluster = builder.build();

      // validate cluster information after connect
      StringBuilder metaDataAfterConnect = new StringBuilder();
      Set<Host> allHosts = cluster.getMetadata().getAllHosts();
      for (Host h : allHosts) {
        metaDataAfterConnect.append("[");
        metaDataAfterConnect.append(h.getDatacenter());
        metaDataAfterConnect.append("-");
        metaDataAfterConnect.append(h.getRack());
        metaDataAfterConnect.append("-");
        metaDataAfterConnect.append(h.getAddress());
        metaDataAfterConnect.append("]\n");
      }

      if (debugEnabled) {
        log.debug("Cassandra Cluster: " + metaDataAfterConnect.toString());
      }

    } catch (NoHostAvailableException ex) {
      throw new DataSourceException(" No Host available to access ", ex);
    } catch (Exception ex) {
      throw new DataSourceException(" Can not create cluster ", ex);
    }

    return cluster;
  }