com.datastax.driver.core.QueryOptions Java Examples

The following examples show how to use com.datastax.driver.core.QueryOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultCommandContext.java    From titus-control-plane with Apache License 2.0 7 votes vote down vote up
public static CommandContext newCommandContext(CommandLine commandLine) {
    List<String> ips = StringExt.splitByComma(commandLine.getOptionValue("H"));
    int sourcePort = Integer.parseInt(commandLine.getOptionValue("p"));

    QueryOptions queryOptions = new QueryOptions()
            .setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);

    Cluster cluster = Cluster.builder()
            .addContactPoints((String[]) ips.toArray())
            .withPort(sourcePort)
            .withQueryOptions(queryOptions)
            .build();

    return new DefaultCommandContext(
            commandLine,
            cluster.newSession(),
            sourceKeySpace -> cluster.connect('"' + sourceKeySpace + '"'),
            targetKeySpace -> cluster.connect('"' + targetKeySpace + '"')
    ) {
        @Override
        public void shutdown() {
            cluster.close();
        }
    };
}
 
Example #2
Source File: CassandraConfig.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
private Builder populateQueryOptions(Map<String, String> properties, Builder builder) {
    String consistencyLevelProp = properties.get(DBConstants.Cassandra.CONSISTENCY_LEVEL);
    String serialConsistencyLevelProp = properties.get(DBConstants.Cassandra.SERIAL_CONSISTENCY_LEVEL);
    String fetchSize = properties.get(DBConstants.Cassandra.FETCH_SIZE);
    QueryOptions options = new QueryOptions();
    if (consistencyLevelProp != null) {
        options.setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevelProp));
    }
    if (serialConsistencyLevelProp != null) {
        options.setSerialConsistencyLevel(ConsistencyLevel.valueOf(serialConsistencyLevelProp));
    }
    if (fetchSize != null) {
        options.setFetchSize(Integer.parseInt(fetchSize));
    }
    return builder.withQueryOptions(options);
}
 
Example #3
Source File: JobSchedulerTest.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
@BeforeSuite
public static void initSuite() {
    Cluster cluster = Cluster.builder()
            .addContactPoints("127.0.0.01")
            .withQueryOptions(new QueryOptions().setRefreshSchemaIntervalMillis(0))
            .build();
    String keyspace = System.getProperty("keyspace", "hawkulartest");
    session = cluster.connect("system");
    rxSession = new RxSessionImpl(session);

    boolean resetdb = Boolean.valueOf(System.getProperty("resetdb", "true"));

    SchemaService schemaService = new SchemaService(session, keyspace);
    schemaService.run( resetdb);

    session.execute("USE " + keyspace);

    jobsService = new JobsService(rxSession);

    findFinishedJobs = session.prepare("SELECT job_id FROM finished_jobs_idx WHERE time_slice = ?");
    getActiveTimeSlices = session.prepare("SELECT distinct time_slice FROM active_time_slices");
    addActiveTimeSlice = session.prepare("INSERT INTO active_time_slices (time_slice) VALUES (?)");
}
 
Example #4
Source File: BaseITest.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
@BeforeSuite
public static void initSuite() {
    String nodeAddresses = System.getProperty("nodes", "127.0.0.1");
    Cluster cluster = new Cluster.Builder()
            .addContactPoints(nodeAddresses.split(","))
            .withQueryOptions(new QueryOptions().setRefreshSchemaIntervalMillis(0))
            .build();
    session = cluster.connect();
    rxSession = new RxSessionImpl(session);

    schemaService = new SchemaService(session, getKeyspace());
    schemaService.run(Boolean.valueOf(System.getProperty("resetdb", "true")));

    session.execute("USE " + getKeyspace());

    jobsManager = new JobsManager(session);

    metricRegistry = new MetricRegistry();
}
 
Example #5
Source File: MapConfiguredCqlClientFactory.java    From storm-cassandra-cql with Apache License 2.0 6 votes vote down vote up
private void configureQueryOptions() {

        final String consistencyConfiguration = (String) configuration.get(TRIDENT_CASSANDRA_CONSISTENCY);
        final String serialConsistencyConfiguration = (String) configuration.get(TRIDENT_CASSANDRA_SERIAL_CONSISTENCY);
        final QueryOptions queryOptions = builder.getConfiguration().getQueryOptions();

        if (StringUtils.isNotEmpty(consistencyConfiguration)) {
            queryOptions.setConsistencyLevel(ConsistencyLevel.valueOf(consistencyConfiguration));
        }

        if (StringUtils.isNotEmpty(serialConsistencyConfiguration)) {
            queryOptions.setSerialConsistencyLevel(ConsistencyLevel.valueOf(serialConsistencyConfiguration));
        }

        builder = builder.withQueryOptions(queryOptions);

    }
 
Example #6
Source File: ExecutionEngine.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
private Session createSession(Profile profile) {
	QueryOptions options = new QueryOptions();
	options.setConsistencyLevel(profile.getConsistencyLevel());
   Cluster.Builder builder = Cluster.builder();
   builder.addContactPoints(profile.getNodes().toArray(new String[0]));
   builder.withPort(profile.getPort());
   builder.withQueryOptions(options);

   if(!StringUtils.isBlank(profile.getUsername()) && !StringUtils.isBlank(profile.getPassword())) {
      builder.withCredentials(profile.getUsername(), profile.getPassword());
   }

   Cluster cluster = builder.build();
   Session session = cluster.connect(profile.getKeyspace());
   return session;
}
 
Example #7
Source File: CassandraClusterCreatorTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateClusterWithConfig() throws Exception {

	CassandraServiceInfo info = new CassandraServiceInfo("local",
			Collections.singletonList("127.0.0.1"), 9142);

	CassandraClusterConfig config = new CassandraClusterConfig();
	config.setCompression(ProtocolOptions.Compression.NONE);
	config.setPoolingOptions(new PoolingOptions().setPoolTimeoutMillis(1234));
	config.setQueryOptions(new QueryOptions());
	config.setProtocolVersion(ProtocolVersion.NEWEST_SUPPORTED);
	config.setLoadBalancingPolicy(new RoundRobinPolicy());
	config.setReconnectionPolicy(new ConstantReconnectionPolicy(1));
	config.setRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
	config.setSocketOptions(new SocketOptions());

	Cluster cluster = creator.create(info, config);

	Configuration configuration = cluster.getConfiguration();

	assertThat(configuration.getProtocolOptions().getCompression(),
			is(config.getCompression()));
	assertThat(configuration.getQueryOptions(), is(config.getQueryOptions()));
	assertThat(configuration.getSocketOptions(), is(config.getSocketOptions()));

	Policies policies = configuration.getPolicies();
	assertThat(policies.getLoadBalancingPolicy(),
			is(config.getLoadBalancingPolicy()));
	assertThat(policies.getReconnectionPolicy(), is(config.getReconnectionPolicy()));
	assertThat(policies.getRetryPolicy(), is(config.getRetryPolicy()));
}
 
Example #8
Source File: CassandraConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
private QueryOptions getQueryOptions(CassandraProperties properties) {
    QueryOptions options = new QueryOptions();
    if (properties.getConsistencyLevel() != null) {
        options.setConsistencyLevel(properties.getConsistencyLevel());
    }
    if (properties.getSerialConsistencyLevel() != null) {
        options.setSerialConsistencyLevel(properties.getSerialConsistencyLevel());
    }
    options.setFetchSize(properties.getFetchSize());
    return options;
}
 
Example #9
Source File: ConstructorConfiguredCqlClientFactory.java    From storm-cassandra-cql with Apache License 2.0 5 votes vote down vote up
public Cluster.Builder getClusterBuilder() {

        final List<InetSocketAddress> sockets = new ArrayList<InetSocketAddress>();
        for (String host : hosts) {
            if(StringUtils.contains(host, ":")) {
                String hostParts [] = StringUtils.split(host, ":");
                sockets.add(new InetSocketAddress(hostParts[0], Integer.valueOf(hostParts[1])));
                LOG.debug("Connecting to [" + host + "] with port [" + hostParts[1] + "]");
            } else {
                sockets.add(new InetSocketAddress(host, ProtocolOptions.DEFAULT_PORT));
                LOG.debug("Connecting to [" + host + "] with port [" + ProtocolOptions.DEFAULT_PORT + "]");
            }
        }

        Cluster.Builder builder = Cluster.builder().addContactPointsWithPorts(sockets).withCompression(compression);
        QueryOptions queryOptions = new QueryOptions();
        queryOptions.setConsistencyLevel(clusterConsistencyLevel);
        queryOptions.setSerialConsistencyLevel(serialConsistencyLevel);
        builder = builder.withQueryOptions(queryOptions);

        if (StringUtils.isNotEmpty(clusterName)) {
            builder = builder.withClusterName(clusterName);
        }

        return builder;

    }
 
Example #10
Source File: CqlSession.java    From ob1k with Apache License 2.0 5 votes vote down vote up
public CqlSession(final String nodes, final int port, final String keyspace, final SocketOptions socketOptions,
                  final RetryPolicy retryPolicy, final QueryOptions queryOptions,
                  final LoadBalancingPolicy loadBalancingPolicy, final int maxConnectionsPerHost,
                  final MetricFactory metricFactory) {

  // this is temp. to reuse current hosts properties:
  final Iterable<String> nodesIter = Splitter.on(",").split(nodes);
  final String[] nodesArr = Iterables.toArray(
    StreamSupport.stream(nodesIter.spliterator(), false).map(input -> {
    if (input == null) return null;

    final int idx = input.lastIndexOf(":");
    return input.substring(0, idx);
  }).collect(Collectors.toList()), String.class);


  /*PoolingOptions poolingOptions = new PoolingOptions();
  poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, maxConnectionsPerHost);
  poolingOptions.setMaxConnectionsPerHost(HostDistance.REMOTE, maxConnectionsPerHost);*/


  final Cluster cluster = Cluster.builder().
          withPort(port).
          withSocketOptions(socketOptions).
          withQueryOptions(queryOptions).
          withLoadBalancingPolicy(loadBalancingPolicy).
          //  withPoolingOptions(poolingOptions).
                  addContactPoints(nodesArr).build();
  //cluster.init();
  this.session = cluster.connect(keyspace);
  this.retryPolicy = Preconditions.checkNotNull(retryPolicy);
  this.metricFactory = Preconditions.checkNotNull(metricFactory);
}
 
Example #11
Source File: CqlConfigHelper.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static QueryOptions getReadQueryOptions(Configuration conf)
{
    String CL = ConfigHelper.getReadConsistencyLevel(conf);
    Optional<Integer> fetchSize = getInputPageRowSize(conf);
    QueryOptions queryOptions = new QueryOptions();
    if (CL != null && !CL.isEmpty())
        queryOptions.setConsistencyLevel(com.datastax.driver.core.ConsistencyLevel.valueOf(CL));

    if (fetchSize.isPresent())
        queryOptions.setFetchSize(fetchSize.get());
    return queryOptions;
}
 
Example #12
Source File: CqlConfigHelper.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static Cluster getInputCluster(String[] hosts, Configuration conf)
{
    int port = getInputNativePort(conf);
    Optional<AuthProvider> authProvider = getAuthProvider(conf);
    Optional<SSLOptions> sslOptions = getSSLOptions(conf);
    Optional<Integer> protocolVersion = getProtocolVersion(conf);
    LoadBalancingPolicy loadBalancingPolicy = getReadLoadBalancingPolicy(conf, hosts);
    SocketOptions socketOptions = getReadSocketOptions(conf);
    QueryOptions queryOptions = getReadQueryOptions(conf);
    PoolingOptions poolingOptions = getReadPoolingOptions(conf);
    
    Cluster.Builder builder = Cluster.builder()
                                     .addContactPoints(hosts)
                                     .withPort(port)
                                     .withCompression(ProtocolOptions.Compression.NONE);

    if (authProvider.isPresent())
        builder.withAuthProvider(authProvider.get());
    if (sslOptions.isPresent())
        builder.withSSL(sslOptions.get());

    if (protocolVersion.isPresent()) {
        builder.withProtocolVersion(protocolVersion.get());
    }
    builder.withLoadBalancingPolicy(loadBalancingPolicy)
           .withSocketOptions(socketOptions)
           .withQueryOptions(queryOptions)
           .withPoolingOptions(poolingOptions);

    return builder.build();
}
 
Example #13
Source File: KeyspaceDropper.java    From usergrid with Apache License 2.0 5 votes vote down vote up
public static void dropTestKeyspace( String keyspace, String[] hosts, int port ) {

        Cluster.Builder builder = Cluster.builder();
        for ( String host : hosts ) {
            builder = builder.addContactPoint( host ).withPort( port );
        }

        final QueryOptions queryOptions = new QueryOptions().setConsistencyLevel( ConsistencyLevel.LOCAL_QUORUM );
        builder.withQueryOptions( queryOptions );
        Cluster cluster = builder.build();

        Session session = cluster.connect();
        logger.info("Dropping test keyspace: {}", keyspace);
        session.execute( "DROP KEYSPACE IF EXISTS " + keyspace );
    }
 
Example #14
Source File: CassandraConnectorITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected Cluster buildCluster(Cluster.Builder builder) {
	return builder
		.addContactPointsWithPorts(new InetSocketAddress(HOST, PORT))
		.withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE).setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL))
		.withoutJMXReporting()
		.withoutMetrics().build();
}
 
Example #15
Source File: Sessions.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static QueryOptions getQueryOptions() {
    QueryOptions queryOptions = new QueryOptions();
    Method setDefaultIdempotenceMethod;
    try {
        setDefaultIdempotenceMethod =
                QueryOptions.class.getMethod("setDefaultIdempotence", boolean.class);
        setDefaultIdempotenceMethod.invoke(queryOptions, true);
    } catch (Exception e) {
        // early version of driver
    }
    return queryOptions;
}
 
Example #16
Source File: Clusters.java    From glowroot with Apache License 2.0 5 votes vote down vote up
static Cluster newCluster() {
    return Cluster.builder().addContactPoint("127.0.0.1")
            // long read timeout is sometimes needed on slow travis ci machines
            .withSocketOptions(new SocketOptions().setReadTimeoutMillis(30000))
            // let driver know that only idempotent queries are used so it will retry on timeout
            .withQueryOptions(new QueryOptions().setDefaultIdempotence(true))
            .build();
}
 
Example #17
Source File: CentralModule.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static Cluster createCluster(CentralConfiguration centralConfig,
        TimestampGenerator defaultTimestampGenerator) {
    Cluster.Builder builder = Cluster.builder()
            .addContactPoints(
                    centralConfig.cassandraContactPoint().toArray(new String[0]))
            .withPort(centralConfig.cassandraPort())
            // aggressive reconnect policy seems ok since not many clients
            .withReconnectionPolicy(new ConstantReconnectionPolicy(1000))
            // let driver know that only idempotent queries are used so it will retry on timeout
            .withQueryOptions(new QueryOptions()
                    .setDefaultIdempotence(true)
                    .setConsistencyLevel(centralConfig.cassandraReadConsistencyLevel()))
            .withPoolingOptions(new PoolingOptions()
                    .setMaxRequestsPerConnection(HostDistance.LOCAL,
                            centralConfig.cassandraMaxConcurrentQueries())
                    .setMaxRequestsPerConnection(HostDistance.REMOTE,
                            centralConfig.cassandraMaxConcurrentQueries())
                    // using 2x "max requests per connection", so that thread-based
                    // throttling can allow up to 3x "max requests per connection", which is
                    // split 50% writes / 25% reads / 25% rollups, which will keep the pool
                    // saturated with writes alone
                    .setMaxQueueSize(centralConfig.cassandraMaxConcurrentQueries() * 2)
                    .setPoolTimeoutMillis(centralConfig.cassandraPoolTimeoutMillis()))
            .withTimestampGenerator(defaultTimestampGenerator);
    String cassandraUsername = centralConfig.cassandraUsername();
    if (!cassandraUsername.isEmpty()) {
        // empty password is strange but valid
        builder.withCredentials(cassandraUsername, centralConfig.cassandraPassword());
    }
    if (centralConfig.cassandraSSL()) {
    	builder.withSSL();
    }
    return builder.build();
}
 
Example #18
Source File: CassandraConnectorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected Cluster buildCluster(Cluster.Builder builder) {
	return builder
		.addContactPointsWithPorts(new InetSocketAddress(HOST, PORT))
		.withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE).setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL))
		.withoutJMXReporting()
		.withoutMetrics().build();
}
 
Example #19
Source File: CassandraSession.java    From eventapis with Apache License 2.0 5 votes vote down vote up
private QueryOptions getQueryOptions() {
    QueryOptions options = new QueryOptions();
    if (eventStoreConfig.getConsistencyLevel() != null) {
        options.setConsistencyLevel(eventStoreConfig.getConsistencyLevel());
    }
    if (eventStoreConfig.getSerialConsistencyLevel() != null) {
        options.setSerialConsistencyLevel(eventStoreConfig.getSerialConsistencyLevel());
    }
    options.setFetchSize(eventStoreConfig.getFetchSize());
    return options;
}
 
Example #20
Source File: CassandraConnectorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected Cluster buildCluster(Cluster.Builder builder) {
	return builder
		.addContactPointsWithPorts(new InetSocketAddress(HOST, PORT))
		.withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE).setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL))
		.withoutJMXReporting()
		.withoutMetrics().build();
}
 
Example #21
Source File: CQLSession.java    From cassandra-sidecar with Apache License 2.0 5 votes vote down vote up
@Inject
public CQLSession(Configuration configuration)
{
    inet = InetSocketAddress.createUnresolved(configuration.getCassandraHost(), configuration.getCassandraPort());
    wlp = new WhiteListPolicy(new RoundRobinPolicy(), Collections.singletonList(inet));
    this.nettyOptions = new NettyOptions();
    this.queryOptions = new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE);
    this.reconnectionPolicy = new ExponentialReconnectionPolicy(1000,
                                                                configuration.getHealthCheckFrequencyMillis());
}
 
Example #22
Source File: CQLSession.java    From cassandra-sidecar with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
CQLSession(InetSocketAddress target, NettyOptions options)
{
    inet = target;
    wlp = new WhiteListPolicy(new RoundRobinPolicy(), Collections.singletonList(inet));
    this.nettyOptions = options;
    this.queryOptions = new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE);
    reconnectionPolicy = new ExponentialReconnectionPolicy(100, 1000);
}
 
Example #23
Source File: CassandraDbProvider.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Currently we connect just once and then reuse the connection.
 * We do not bother with closing the connection.
 *
 * It is normal to use one Session per DB. The Session is thread safe.
 */
private void connect() {

    if (cluster == null) {

        log.info("Connecting to Cassandra server on " + this.dbHost + " at port " + this.dbPort);

        // allow fetching as much data as present in the DB
        QueryOptions queryOptions = new QueryOptions();
        queryOptions.setFetchSize(Integer.MAX_VALUE);
        queryOptions.setConsistencyLevel(ConsistencyLevel.ONE);

        cluster = Cluster.builder()
                         .addContactPoint(this.dbHost)
                         .withPort(this.dbPort)
                         .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()))
                         .withReconnectionPolicy(new ExponentialReconnectionPolicy(500, 30000))
                         .withQueryOptions(queryOptions)
                         .withCredentials(this.dbUser, this.dbPassword)
                         .build();

    }

    if (session == null) {

        log.info("Connecting to Cassandra DB with name " + this.dbName);
        session = cluster.connect(dbName);
    }
}
 
Example #24
Source File: CassandraStorage.java    From cassandra-reaper with Apache License 2.0 5 votes vote down vote up
private static void overrideQueryOptions(CassandraFactory cassandraFactory) {
  // all INSERT and DELETE stmt prepared in this class are idempoten
  if (cassandraFactory.getQueryOptions().isPresent()
      && ConsistencyLevel.LOCAL_ONE != cassandraFactory.getQueryOptions().get().getConsistencyLevel()) {
    LOG.warn("Customization of cassandra's queryOptions is not supported and will be overridden");
  }
  cassandraFactory.setQueryOptions(java.util.Optional.of(new QueryOptions().setDefaultIdempotence(true)));
}
 
Example #25
Source File: ManagedSetupConnection.java    From heroic with Apache License 2.0 5 votes vote down vote up
public AsyncFuture<Connection> construct() {
    AsyncFuture<Session> session = async.call(() -> {


        final QueryOptions queryOptions = new QueryOptions()
            .setFetchSize(fetchSize)
            .setConsistencyLevel(consistencyLevel);

        final SocketOptions socketOptions = new SocketOptions()
            .setReadTimeoutMillis((int) readTimeout.toMilliseconds());

        final Cluster.Builder cluster = Cluster.builder()
            .addContactPointsWithPorts(seeds)
            .withRetryPolicy(retryPolicy)
            .withQueryOptions(queryOptions)
            .withSocketOptions(socketOptions)
            .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()))
            .withoutJMXReporting();

        authentication.accept(cluster);
        poolingOptions.apply(cluster);

        return cluster.build().connect();
    });

    if (configure) {
        session = session.lazyTransform(s -> schema.configure(s).directTransform(i -> s));
    }

    return session.lazyTransform(
        s -> schema.instance(s).directTransform(schema -> new Connection(s, schema)));
}
 
Example #26
Source File: CassandraTarget.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private Cluster getCluster() throws StageException {
  RemoteEndpointAwareJdkSSLOptions sslOptions = null;

  if (conf.tlsConfig.isEnabled()) {
    // Not certain why we need the downcast here, but we do
    sslOptions = (RemoteEndpointAwareJdkSSLOptions)RemoteEndpointAwareJdkSSLOptions.builder()
        .withSSLContext(conf.tlsConfig.getSslContext())
        .build();
  }

  SocketOptions socketOptions = new SocketOptions();
  socketOptions.setConnectTimeoutMillis(conf.connectionTimeout);
  socketOptions.setReadTimeoutMillis(conf.readTimeout);

  QueryOptions queryOptions = new QueryOptions();
  queryOptions.setConsistencyLevel(conf.consistencyLevel);

  Cluster cluster = Cluster.builder()
      .addContactPoints(contactPoints)
      .withSSL(sslOptions)
      // If authentication is disabled on the C* cluster, this method has no effect.
      .withAuthProvider(getAuthProvider())
      .withProtocolVersion(conf.protocolVersion)
      .withPort(conf.port)
      .withCodecRegistry(new CodecRegistry().register(SDC_CODECS))
      .withSocketOptions(socketOptions)
      .withQueryOptions(queryOptions)
      .build();

  if (conf.logSlowQueries) {
    QueryLogger queryLogger = QueryLogger.builder()
        .withConstantThreshold(conf.slowQueryThreshold)
        .build();
    cluster.register(queryLogger);
  }
  return cluster;
}
 
Example #27
Source File: BuildCluster.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
public static boolean isClusterActive(){
	try{
		Builder builder = Cluster.builder().withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.QUORUM).setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL));
		cluster = builder.addContactPoint("127.0.0.1").build();
		session = cluster.connect();
		return true;
	} catch(Exception e){
		return false;			
	}
	
}
 
Example #28
Source File: CassandraIO.java    From beam with Apache License 2.0 4 votes vote down vote up
/** Get a Cassandra cluster using hosts and port. */
private static Cluster getCluster(
    ValueProvider<List<String>> hosts,
    ValueProvider<Integer> port,
    ValueProvider<String> username,
    ValueProvider<String> password,
    ValueProvider<String> localDc,
    ValueProvider<String> consistencyLevel,
    ValueProvider<Integer> connectTimeout,
    ValueProvider<Integer> readTimeout) {

  Cluster.Builder builder =
      Cluster.builder().addContactPoints(hosts.get().toArray(new String[0])).withPort(port.get());

  if (username != null) {
    builder.withAuthProvider(new PlainTextAuthProvider(username.get(), password.get()));
  }

  DCAwareRoundRobinPolicy.Builder dcAwarePolicyBuilder = new DCAwareRoundRobinPolicy.Builder();
  if (localDc != null) {
    dcAwarePolicyBuilder.withLocalDc(localDc.get());
  }

  builder.withLoadBalancingPolicy(new TokenAwarePolicy(dcAwarePolicyBuilder.build()));

  if (consistencyLevel != null) {
    builder.withQueryOptions(
        new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel.get())));
  }

  SocketOptions socketOptions = new SocketOptions();

  builder.withSocketOptions(socketOptions);

  if (connectTimeout != null) {
    socketOptions.setConnectTimeoutMillis(connectTimeout.get());
  }

  if (readTimeout != null) {
    socketOptions.setReadTimeoutMillis(readTimeout.get());
  }

  return builder.build();
}
 
Example #29
Source File: CassandraQueryOptions.java    From iotplatform with Apache License 2.0 4 votes vote down vote up
@PostConstruct
public void initOpts(){
    opts = new QueryOptions();
    opts.setFetchSize(defaultFetchSize);
}
 
Example #30
Source File: CassandraCluster.java    From monasca-persister with Apache License 2.0 4 votes vote down vote up
@Inject
public CassandraCluster(final PersisterConfig config) {

  this.dbConfig = config.getCassandraDbConfiguration();

  QueryOptions qo = new QueryOptions();
  qo.setConsistencyLevel(ConsistencyLevel.valueOf(dbConfig.getConsistencyLevel()));
  qo.setDefaultIdempotence(true);

  String[] contactPoints = dbConfig.getContactPoints();
  int retries = dbConfig.getMaxWriteRetries();
  Builder builder = Cluster.builder().addContactPoints(contactPoints).withPort(dbConfig.getPort());
  builder
      .withSocketOptions(new SocketOptions().setConnectTimeoutMillis(dbConfig.getConnectionTimeout())
          .setReadTimeoutMillis(dbConfig.getReadTimeout()));
  builder.withQueryOptions(qo).withRetryPolicy(new MonascaRetryPolicy(retries, retries, retries));

  lbPolicy = new TokenAwarePolicy(
      DCAwareRoundRobinPolicy.builder().withLocalDc(dbConfig.getLocalDataCenter()).build());
  builder.withLoadBalancingPolicy(lbPolicy);

  String user = dbConfig.getUser();
  if (user != null && !user.isEmpty()) {
    builder.withAuthProvider(new PlainTextAuthProvider(dbConfig.getUser(), dbConfig.getPassword()));
  }
  cluster = builder.build();

  PoolingOptions poolingOptions = cluster.getConfiguration().getPoolingOptions();

  poolingOptions.setConnectionsPerHost(HostDistance.LOCAL, dbConfig.getMaxConnections(),
      dbConfig.getMaxConnections()).setConnectionsPerHost(HostDistance.REMOTE,
          dbConfig.getMaxConnections(), dbConfig.getMaxConnections());

  poolingOptions.setMaxRequestsPerConnection(HostDistance.LOCAL, dbConfig.getMaxRequests())
      .setMaxRequestsPerConnection(HostDistance.REMOTE, dbConfig.getMaxRequests());

  metricsSession = cluster.connect(dbConfig.getKeySpace());

  measurementInsertStmt = metricsSession.prepare(MEASUREMENT_INSERT_CQL).setIdempotent(true);
  // TODO: Remove update statements, TTL issues
  measurementUpdateStmt = metricsSession.prepare(MEASUREMENT_UPDATE_CQL).setIdempotent(true);
  metricInsertStmt = metricsSession.prepare(METRICS_INSERT_CQL).setIdempotent(true);
  dimensionStmt = metricsSession.prepare(DIMENSION_INSERT_CQL).setIdempotent(true);
  dimensionMetricStmt = metricsSession.prepare(DIMENSION_METRIC_INSERT_CQL).setIdempotent(true);
  metricDimensionStmt = metricsSession.prepare(METRIC_DIMENSION_INSERT_CQL).setIdempotent(true);

  retrieveMetricIdStmt = metricsSession.prepare(RETRIEVE_METRIC_ID_CQL).setIdempotent(true);
  retrieveMetricDimensionStmt = metricsSession.prepare(RETRIEVE_METRIC_DIMENSION_CQL)
      .setIdempotent(true);

  alarmsSession = cluster.connect(dbConfig.getKeySpace());

  alarmHistoryInsertStmt = alarmsSession.prepare(INSERT_ALARM_STATE_HISTORY_SQL).setIdempotent(true);

  metricIdCache = CacheBuilder.newBuilder()
      .maximumSize(config.getCassandraDbConfiguration().getDefinitionMaxCacheSize()).build();

  dimensionCache = CacheBuilder.newBuilder()
      .maximumSize(config.getCassandraDbConfiguration().getDefinitionMaxCacheSize()).build();

  metricDimensionCache = CacheBuilder.newBuilder()
      .maximumSize(config.getCassandraDbConfiguration().getDefinitionMaxCacheSize()).build();

  logger.info("loading cached definitions from db");

  ExecutorService executor = Executors.newFixedThreadPool(250);

  //a majority of the ids are for metrics not actively receiving msgs anymore
  //loadMetricIdCache(executor);

  loadDimensionCache();

  loadMetricDimensionCache(executor);

  executor.shutdown();
}