Search in sources :

Example 81 with Cluster

use of com.datastax.driver.core.Cluster in project beam by apache.

the class CassandraIOIT method dropTable.

private static void dropTable(CassandraIOITOptions options, String keyspace, String table) {
    try (Cluster cluster = getCluster(options);
        Session session = cluster.connect()) {
        session.execute("DROP KEYSPACE IF EXISTS " + keyspace);
        session.execute("DROP TABLE IF EXISTS " + keyspace + "." + table);
    }
}
Also used : Cluster(com.datastax.driver.core.Cluster) Session(com.datastax.driver.core.Session)

Example 82 with Cluster

use of com.datastax.driver.core.Cluster in project YCSB by brianfrankcooper.

the class CassandraCQLClient method init.

/**
 * Initialize any state for this DB. Called once per DB instance; there is one
 * DB instance per client thread.
 */
@Override
public void init() throws DBException {
    // Keep track of number of calls to init (for later cleanup)
    INIT_COUNT.incrementAndGet();
    // cluster/session instance for all the threads.
    synchronized (INIT_COUNT) {
        // Check if the cluster has already been initialized
        if (cluster != null) {
            return;
        }
        try {
            debug = Boolean.parseBoolean(getProperties().getProperty("debug", "false"));
            trace = Boolean.valueOf(getProperties().getProperty(TRACING_PROPERTY, TRACING_PROPERTY_DEFAULT));
            String host = getProperties().getProperty(HOSTS_PROPERTY);
            if (host == null) {
                throw new DBException(String.format("Required property \"%s\" missing for CassandraCQLClient", HOSTS_PROPERTY));
            }
            String[] hosts = host.split(",");
            String port = getProperties().getProperty(PORT_PROPERTY, PORT_PROPERTY_DEFAULT);
            String username = getProperties().getProperty(USERNAME_PROPERTY);
            String password = getProperties().getProperty(PASSWORD_PROPERTY);
            String keyspace = getProperties().getProperty(KEYSPACE_PROPERTY, KEYSPACE_PROPERTY_DEFAULT);
            readConsistencyLevel = ConsistencyLevel.valueOf(getProperties().getProperty(READ_CONSISTENCY_LEVEL_PROPERTY, READ_CONSISTENCY_LEVEL_PROPERTY_DEFAULT));
            writeConsistencyLevel = ConsistencyLevel.valueOf(getProperties().getProperty(WRITE_CONSISTENCY_LEVEL_PROPERTY, WRITE_CONSISTENCY_LEVEL_PROPERTY_DEFAULT));
            Boolean useSSL = Boolean.parseBoolean(getProperties().getProperty(USE_SSL_CONNECTION, DEFAULT_USE_SSL_CONNECTION));
            if ((username != null) && !username.isEmpty()) {
                Cluster.Builder clusterBuilder = Cluster.builder().withCredentials(username, password).withPort(Integer.valueOf(port)).addContactPoints(hosts);
                if (useSSL) {
                    clusterBuilder = clusterBuilder.withSSL();
                }
                cluster = clusterBuilder.build();
            } else {
                cluster = Cluster.builder().withPort(Integer.valueOf(port)).addContactPoints(hosts).build();
            }
            String maxConnections = getProperties().getProperty(MAX_CONNECTIONS_PROPERTY);
            if (maxConnections != null) {
                cluster.getConfiguration().getPoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL, Integer.valueOf(maxConnections));
            }
            String coreConnections = getProperties().getProperty(CORE_CONNECTIONS_PROPERTY);
            if (coreConnections != null) {
                cluster.getConfiguration().getPoolingOptions().setCoreConnectionsPerHost(HostDistance.LOCAL, Integer.valueOf(coreConnections));
            }
            String connectTimoutMillis = getProperties().getProperty(CONNECT_TIMEOUT_MILLIS_PROPERTY);
            if (connectTimoutMillis != null) {
                cluster.getConfiguration().getSocketOptions().setConnectTimeoutMillis(Integer.valueOf(connectTimoutMillis));
            }
            String readTimoutMillis = getProperties().getProperty(READ_TIMEOUT_MILLIS_PROPERTY);
            if (readTimoutMillis != null) {
                cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(Integer.valueOf(readTimoutMillis));
            }
            Metadata metadata = cluster.getMetadata();
            logger.info("Connected to cluster: {}\n", metadata.getClusterName());
            for (Host discoveredHost : metadata.getAllHosts()) {
                logger.info("Datacenter: {}; Host: {}; Rack: {}\n", discoveredHost.getDatacenter(), discoveredHost.getAddress(), discoveredHost.getRack());
            }
            session = cluster.connect(keyspace);
        } catch (Exception e) {
            throw new DBException(e);
        }
    }
// synchronized
}
Also used : DBException(site.ycsb.DBException) Metadata(com.datastax.driver.core.Metadata) Cluster(com.datastax.driver.core.Cluster) Host(com.datastax.driver.core.Host) DBException(site.ycsb.DBException)

Example 83 with Cluster

use of com.datastax.driver.core.Cluster in project tutorials by eugenp.

the class CassandraTemplateIntegrationTest method startCassandraEmbedded.

// 
@BeforeClass
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
    EmbeddedCassandraServerHelper.startEmbeddedCassandra();
    final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
    LOGGER.info("Server Started at 127.0.0.1:9142... ");
    final Session session = cluster.connect();
    session.execute(KEYSPACE_CREATION_QUERY);
    session.execute(KEYSPACE_ACTIVATE_QUERY);
    LOGGER.info("KeySpace created and activated.");
    Thread.sleep(5000);
}
Also used : Cluster(com.datastax.driver.core.Cluster) Session(com.datastax.driver.core.Session) BeforeClass(org.junit.BeforeClass)

Example 84 with Cluster

use of com.datastax.driver.core.Cluster in project ASAP by salmant.

the class SS1 method Fetch_AVG_response_time_metric.

// ///////////////////////////////////////////
public static double Fetch_AVG_response_time_metric(String metric_name, String haproxy_agentid, String event_date) {
    Cluster cluster;
    Session session;
    cluster = Cluster.builder().addContactPoints("194.249.1.175").withCredentials("catascopia_user", "catascopia_pass").withPort(9042).withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE).withReconnectionPolicy(new ConstantReconnectionPolicy(1000L)).build();
    session = cluster.connect("jcatascopiadb");
    haproxy_agentid = haproxy_agentid + ":" + metric_name;
    ResultSet results = session.execute("select value from jcatascopiadb.metric_value_table where metricid=\'" + haproxy_agentid + "\' and event_date=\'" + event_date + "\' ORDER BY event_timestamp DESC LIMIT 1");
    double AVG_response_time_metric = 0.0;
    for (Row row : results) {
        AVG_response_time_metric = Double.parseDouble(row.getString("value"));
    }
    return Math.abs(AVG_response_time_metric);
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Cluster(com.datastax.driver.core.Cluster) Row(com.datastax.driver.core.Row) Session(com.datastax.driver.core.Session) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy)

Example 85 with Cluster

use of com.datastax.driver.core.Cluster in project ASAP by salmant.

the class SS1 method Fetch_request_rate_metric.

// ///////////////////////////////////////////
public static double Fetch_request_rate_metric(String metric_name, String haproxy_agentid, String event_date) {
    Cluster cluster;
    Session session;
    cluster = Cluster.builder().addContactPoints("194.249.1.175").withCredentials("catascopia_user", "catascopia_pass").withPort(9042).withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE).withReconnectionPolicy(new ConstantReconnectionPolicy(1000L)).build();
    session = cluster.connect("jcatascopiadb");
    haproxy_agentid = haproxy_agentid + ":" + metric_name;
    ResultSet results = session.execute("select value from jcatascopiadb.metric_value_table where metricid=\'" + haproxy_agentid + "\' and event_date=\'" + event_date + "\' ORDER BY event_timestamp DESC LIMIT 1");
    double GoodPut = 0.0;
    for (Row row : results) {
        GoodPut = Double.parseDouble(row.getString("value"));
    }
    return Math.abs(GoodPut);
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Cluster(com.datastax.driver.core.Cluster) Row(com.datastax.driver.core.Row) Session(com.datastax.driver.core.Session) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy)

Aggregations

Cluster (com.datastax.driver.core.Cluster)123 Session (com.datastax.driver.core.Session)74 ResultSet (com.datastax.driver.core.ResultSet)41 Row (com.datastax.driver.core.Row)37 Test (org.junit.Test)33 ConstantReconnectionPolicy (com.datastax.driver.core.policies.ConstantReconnectionPolicy)30 Host (com.datastax.driver.core.Host)10 Metadata (com.datastax.driver.core.Metadata)9 PreparedStatement (com.datastax.driver.core.PreparedStatement)9 ArrayList (java.util.ArrayList)9 LoadBalancingPolicy (com.datastax.driver.core.policies.LoadBalancingPolicy)8 IOException (java.io.IOException)8 PoolingOptions (com.datastax.driver.core.PoolingOptions)7 ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)7 BeforeClass (org.junit.BeforeClass)7 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)6 QueryOptions (com.datastax.driver.core.QueryOptions)6 InetSocketAddress (java.net.InetSocketAddress)6 BoundStatement (com.datastax.driver.core.BoundStatement)5 SocketOptions (com.datastax.driver.core.SocketOptions)5