static Session connect() { String contactPoint = "localhost"; String keySpace = "ks1"; if(session == null) { PoolingOptions poolingOptions = new PoolingOptions().setConnectionsPerHost(HostDistance.REMOTE, 1, 4); cluster = Cluster.builder().addContactPoint(contactPoint).withPoolingOptions(poolingOptions) .withCompression(Compression.SNAPPY).build(); cluster.init(); for (Host host : cluster.getMetadata().getAllHosts()) { System.out.printf("Address: %s, Rack: %s, Datacenter: %s, Tokens: %s\n", host.getAddress(), host.getDatacenter(), host.getRack(), host.getTokens()); } session = cluster.connect(keySpace); } return session; }
protected void init(String keyspaceName) { this.keyspaceName = keyspaceName; Cluster.Builder builder = Cluster.builder() .addContactPointsWithPorts(getContactPoints(url)) .withClusterName(clusterName) .withSocketOptions(socketOpts.getOpts()) .withPoolingOptions(new PoolingOptions() .setMaxRequestsPerConnection(HostDistance.LOCAL, 32768) .setMaxRequestsPerConnection(HostDistance.REMOTE, 32768)); builder.withQueryOptions(queryOpts.getOpts()); builder.withCompression(StringUtils.isEmpty(compression) ? Compression.NONE : Compression.valueOf(compression.toUpperCase())); if (ssl) { builder.withSSL(); } if (!jmx) { builder.withoutJMXReporting(); } if (!metrics) { builder.withoutMetrics(); } if (credentials) { builder.withCredentials(username, password); } cluster = builder.build(); cluster.init(); if (!isInstall()) { initSession(); } }
protected void init(String keyspaceName) { this.keyspaceName = keyspaceName; this.clusterBuilder = Cluster.builder() .addContactPointsWithPorts(getContactPoints(url)) .withClusterName(clusterName) .withSocketOptions(socketOpts.getOpts()) .withPoolingOptions(new PoolingOptions() .setMaxRequestsPerConnection(HostDistance.LOCAL, 32768) .setMaxRequestsPerConnection(HostDistance.REMOTE, 32768)); this.clusterBuilder.withQueryOptions(queryOpts.getOpts()); this.clusterBuilder.withCompression(StringUtils.isEmpty(compression) ? Compression.NONE : Compression.valueOf(compression.toUpperCase())); if (ssl) { this.clusterBuilder.withSSL(); } if (!jmx) { this.clusterBuilder.withoutJMXReporting(); } if (!metrics) { this.clusterBuilder.withoutMetrics(); } if (credentials) { this.clusterBuilder.withCredentials(username, password); } if (!isInstall()) { initSession(); } }
@Override public void open() { final String[] addresses = getProperty(CASSANDRA_HOSTS).split(","); final int port = parseInt(getProperty(CASSANDRA_PORT)); StringBuilder hosts = new StringBuilder(); for (String address : addresses) { hosts.append(address).append(","); } LOGGER.info("Bootstrapping Cassandra Java Driver to connect to " + hosts.toString() + "on port " + port); Compression compression = driverConfig.getCompressionProtocol(this); cluster = Cluster.builder() .addContactPoints(addresses) .withPort(port) .withProtocolVersion(driverConfig.getProtocolVersion(this)) .withClusterName(getProperty(CASSANDRA_CLUSTER_NAME)) .withCompression(compression) .withCredentials(getProperty(CASSANDRA_CREDENTIALS_USERNAME), getProperty(CASSANDRA_CREDENTIALS_PASSWORD)) .withLoadBalancingPolicy(driverConfig.getLoadBalancingPolicy(this)) .withRetryPolicy(driverConfig.getRetryPolicy(this)) .withReconnectionPolicy(driverConfig.getReconnectionPolicy(this)) .withSpeculativeExecutionPolicy(driverConfig.getSpeculativeExecutionPolicy(this)) .withMaxSchemaAgreementWaitSeconds( parseInt(getProperty(CASSANDRA_MAX_SCHEMA_AGREEMENT_WAIT_SECONDS))) .withPoolingOptions(driverConfig.getPoolingOptions(this)) .withQueryOptions(driverConfig.getQueryOptions(this)) .withSocketOptions(driverConfig.getSocketOptions(this)) .build(); session = cluster.connect(); helper = new InterpreterLogic(session); }
@Override public void accept(final Configuration<Map<String, Object>> configuration) { synchronized (CLUSTER_LOCK) { if (cluster == null) { final String hosts = configuration.getAttribute(HOSTS, "localhost"); final Compression compression = compression(configuration); final Cluster.Builder builder = Cluster.builder() .addContactPoints(hosts.split("\\s*,\\s*")) .withRetryPolicy(retryPolicy(configuration)) .withReconnectionPolicy(reconnectionPolicy(configuration)) .withLoadBalancingPolicy(loadBalancingPolicy(configuration)) .withPoolingOptions(poolingOptions(configuration)) .withQueryOptions(queryOptions(configuration)) .withSocketOptions(socketOptions(configuration)); if (compression != null) { builder.withCompression(compression); } cluster = builder.build(); LOGGER.debug(MessageCatalog._00107_CONNECTED_TO_CLUSTER); } } _session = cluster.connect(); _keyspaceName = configuration.getAttribute(KEYSPACE, "KeyspaceCumulus") + _keyspaceNameSuffix; _replicationFactor = configuration.getAttribute(REPLICATION_FACTOR, Integer.valueOf(1)); _ttl = configuration.getAttribute(TTL, Integer.valueOf(-1)); }
/** * Returns the compression options according with a given configuration. * * @param configuration the configuration. * @return the compression options according with a given configuration. */ private Compression configureCompression(final Configuration<Map<String, Object>> configuration) { Compression compression = null; try { final String compressionOption = configuration.getParameter("transport-compression", null); if (compressionOption != null) { compression = Compression.valueOf(compressionOption); } } catch (final Exception e) { // Ignore and don't set the compression. } return compression; }
@Inject public CassandraSessionImpl(@Named("cassandra.keyspace") String keyspace, @Named("cassandra.hostname") String hostname, @Named("cassandra.port") int port, @Named("cassandra.compression") String compression, @Named("cassandra.username") String username, @Named("cassandra.password") String password, @Named("cassandra.ssl") boolean ssl) { checkNotNull(keyspace, "keyspace argument"); checkNotNull(hostname, "hostname argument"); checkArgument(port > 0 && port < 65535, "not a valid port number: %d", port); checkNotNull(compression, "compression argument"); LOG.info("Setting up session with {}:{} using compression {}", hostname, port, compression.toUpperCase()); Builder builder = Cluster .builder() .withPort(port) .addContactPoints(hostname.split(",")) .withReconnectionPolicy(new ExponentialReconnectionPolicy(1000, 2 * 60 * 1000)) .withCompression(Compression.valueOf(compression.toUpperCase())); if (username != null && password != null) { LOG.info("Using username: {} and password: XXXXXXXX", username); builder.withCredentials(username, password); } if (ssl) { LOG.info("Enabling SSL."); builder.withSSL(); } m_session = builder.build().connect(keyspace); }
private Compression parseCompression(String cmp) { Compression compression = Compression.NONE; if (StringUtils.isNotBlank(cmp)) { for (Compression current : Compression.values()) { if (current.name().equalsIgnoreCase(cmp)) { compression = current; break; } } } return compression; }
private Cluster buildClusterSpecs() { Cluster.Builder builder = Cluster.builder(); // dbhost String dbhost = getParamString("dbhost"); String[] nodeAddresses = dbhost.split(","); for (String address : nodeAddresses) { builder.addContactPoint(address); } // dbport builder.withPort(getParamInt("dbport", 9042)); // db_timeout_millis and db_connect_retry_wait_millis SocketOptions socketOpts = new SocketOptions(); socketOpts.setReadTimeoutMillis(getParamInt("db_timeout_millis", 10000)); socketOpts.setConnectTimeoutMillis(getParamInt("db_connect_retry_wait_millis", 5000)); builder.withSocketOptions(socketOpts); // dbuser/dbpassword String dbuser = getParamString("dbuser"); if (!Utils.isEmpty(dbuser)) { builder.withCredentials(dbuser, getParamString("dbpassword")); } // compression builder.withCompression(Compression.SNAPPY); // TLS/SSL if (getParamBoolean("dbtls")) { builder.withSSL(getSSLOptions()); } return builder.build(); }
private static Cluster build(List<String> addresses, int port) { Cluster.Builder builder = Cluster.builder(); for (String address : addresses) { builder.addContactPoint(address); } builder .withPort(port) .withCompression(Compression.LZ4) .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE) .withLoadBalancingPolicy(LatencyAwarePolicy.builder(new RoundRobinPolicy()).build()); Cluster cluster = builder.build(); try { // Attempt to init the cluster to make sure it's usable. I'd prefer to remove this and leave it on the // client to retry when the connect method throws an exception. cluster.init(); return cluster; } catch(NoHostAvailableException e) { LOGGER.warn("Unable to connect to Cassandra, will retry contact points next time", cluster, e); cluster = builder.build(); cluster.init(); } return cluster; }
public Compression getCompression() { return this.compression; }
public void setCompression(Compression compression) { this.compression = compression; }
@Override public void open() { final String[] addresses = getProperty(CASSANDRA_HOSTS).split(","); final int port = parseInt(getProperty(CASSANDRA_PORT)); StringBuilder hosts = new StringBuilder(); for (String address : addresses) { hosts.append(address).append(","); } LOGGER.info("Bootstrapping Cassandra Java Driver to connect to " + hosts.toString() + "on port " + port); Compression compression = driverConfig.getCompressionProtocol(this); clusterBuilder = Cluster.builder() .addContactPoints(addresses) .withPort(port) .withProtocolVersion(driverConfig.getProtocolVersion(this)) .withClusterName(getProperty(CASSANDRA_CLUSTER_NAME)) .withCompression(compression) .withCredentials(getProperty(CASSANDRA_CREDENTIALS_USERNAME), getProperty(CASSANDRA_CREDENTIALS_PASSWORD)) .withLoadBalancingPolicy(driverConfig.getLoadBalancingPolicy(this)) .withRetryPolicy(driverConfig.getRetryPolicy(this)) .withReconnectionPolicy(driverConfig.getReconnectionPolicy(this)) .withSpeculativeExecutionPolicy(driverConfig.getSpeculativeExecutionPolicy(this)) .withMaxSchemaAgreementWaitSeconds( parseInt(getProperty(CASSANDRA_MAX_SCHEMA_AGREEMENT_WAIT_SECONDS))) .withPoolingOptions(driverConfig.getPoolingOptions(this)) .withQueryOptions(driverConfig.getQueryOptions(this)) .withSocketOptions(driverConfig.getSocketOptions(this)); final String runWithSSL = getProperty(CASSANDRA_WITH_SSL); if (runWithSSL != null && runWithSSL.equals("true")) { LOGGER.debug("Cassandra Interpreter: Using SSL"); try { final SSLContext sslContext; { final KeyStore trustStore = KeyStore.getInstance("JKS"); final InputStream stream = Files.newInputStream(Paths.get( getProperty(CASSANDRA_TRUSTSTORE_PATH))); trustStore.load(stream, getProperty(CASSANDRA_TRUSTSTORE_PASSWORD).toCharArray()); final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(trustStore); sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, trustManagerFactory.getTrustManagers(), null); } clusterBuilder = clusterBuilder.withSSL(JdkSSLOptions.builder() .withSSLContext(sslContext) .build()); } catch (Exception e) { LOGGER.error(e.toString()); } } else { LOGGER.debug("Cassandra Interpreter: Not using SSL"); } cluster = clusterBuilder.build(); session = cluster.connect(); helper = new InterpreterLogic(session); }
public CQLClient(LoaderOptions options, String keyspace) throws NoSuchAlgorithmException, FileNotFoundException, IOException, KeyStoreException, CertificateException, UnrecoverableKeyException, KeyManagementException, ConfigurationException { // System.setProperty("com.datastax.driver.NON_BLOCKING_EXECUTOR_SIZE", // "64"); PoolingOptions poolingOptions = new PoolingOptions(); int connections = options.connectionsPerHost; if (connections == 0) { connections = 8; } poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, Math.max(1, connections / 2)); poolingOptions.setCoreConnectionsPerHost(HostDistance.REMOTE, Math.max(1, connections / 4)); poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, connections); poolingOptions.setMaxConnectionsPerHost(HostDistance.REMOTE, Math.max(1, connections / 2)); poolingOptions.setMaxRequestsPerConnection(HostDistance.LOCAL, 32768); poolingOptions.setMaxRequestsPerConnection(HostDistance.REMOTE, 2000); this.simulate = options.simulate; this.verbose = options.verbose; Cluster.Builder builder = builder().addContactPoints(options.hosts).withProtocolVersion(ProtocolVersion.V3) .withCompression(Compression.LZ4).withPoolingOptions(poolingOptions); if (options.user != null && options.passwd != null) { builder = builder.withCredentials(options.user, options.passwd); } if (options.ssl) { EncryptionOptions enco = options.encOptions; SSLContext ctx = SSLContext.getInstance(options.encOptions.protocol); try (FileInputStream tsf = new FileInputStream(enco.truststore); FileInputStream ksf = new FileInputStream(enco.keystore)) { KeyStore ts = KeyStore.getInstance(enco.store_type); ts.load(tsf, enco.truststore_password.toCharArray()); TrustManagerFactory tmf = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(ts); KeyStore ks = KeyStore.getInstance("JKS"); ks.load(ksf, enco.keystore_password.toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, enco.keystore_password.toCharArray()); ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom()); } SSLOptions sslOptions = JdkSSLOptions.builder().withSSLContext(ctx).withCipherSuites(enco.cipher_suites) .build(); builder = builder.withSSL(sslOptions); } cluster = builder.build(); session = cluster.connect(keyspace); metadata = cluster.getMetadata(); keyspaceMetadata = metadata.getKeyspace(keyspace); org.apache.cassandra.schema.KeyspaceMetadata ksMetaData = org.apache.cassandra.schema.KeyspaceMetadata .create(keyspaceMetadata.getName(), KeyspaceParams.create(keyspaceMetadata.isDurableWrites(), keyspaceMetadata.getReplication())); Schema.instance.load(ksMetaData); loadUserTypes(keyspaceMetadata.getUserTypes(), keyspace); partitioner = FBUtilities.newPartitioner(metadata.getPartitioner()); if (options.throttle != 0) { rateLimiter = RateLimiter.create(options.throttle * 1000 * 1000 / 8); } this.batch = options.batch; this.preparedStatements = options.prepare ? new ConcurrentHashMap<>() : null; this.ignoreColumns = options.ignoreColumns; }
@Override public void accept(final Configuration<Map<String, Object>> configuration) { deletionBatchSize = configuration.getParameter("delete-batch-size", Integer.valueOf(1000)); final String hosts = configuration.getParameter("cassandra-contact-points", "localhost"); final Cluster.Builder builder = Cluster.builder() .addContactPoints(hosts.split(",")) .withLoadBalancingPolicy(configureLoadBalancingPolicy(configuration)) .withQueryOptions(configureQueryOptions(configuration)) .withPoolingOptions(configurePoolingOptions(configuration)) .withReconnectionPolicy(configureReconnectionPolicy(configuration)) .withRetryPolicy(configureRetryPolicy(configuration)) .withSocketOptions(configureSocketOptions(configuration)); final Compression compression = configureCompression(configuration); if (compression != null) { builder.withCompression(compression); } final String keyspaceName = configuration.getParameter("keyspace-name", "C2XDB"); final Boolean createSchema = configuration.getParameter("create-schema", Boolean.TRUE); cluster = builder.build(); final Metadata metadata = cluster.getMetadata(); final KeyspaceMetadata keyspaceMetadata = metadata.getKeyspace(keyspaceName); session = cluster.connect(); if (keyspaceMetadata == null) { createKeyspace(keyspaceName, configuration); session.execute("USE " + keyspaceName); if (createSchema) { createSchema(configuration); } } else { session.execute("USE " + keyspaceName); } dictionary = new TransientNodeDictionary("TEST_DICTIONARY", new TransientNodeDictionary("NEVER_USED"), 1000000); // new CacheNodectionary( // "TopLevelCacheDictionary", // new KnownURIsDictionary( // "KnownURIsDictionary", // new ThreeTieredNodeDictionary( // "ThreeTieredDictionary", // new CacheStringDictionary( // "NamespacesCacheDictionary", // new PersistentStringDictionary("NamespacesDictionary", "DICT_NAMESPACES"), // configuration.getParameter("namespaces-id-cache-size", Integer.valueOf(1000)), // configuration.getParameter("namespaces-value-cache-size", Integer.valueOf(1000)), // false), // new TransientStringDictionary("LocalNamesDictionary"), // new CacheNodectionary( // "LiteralsAndBNodesCacheDictionary", // new TransientNodeDictionary( // "LiteralAndBNodesDictionary", // new PersistentNodeDictionary("LongLiteralsDictionary"), // configuration.getParameter("long-literals-threshold", Integer.valueOf(1000))), // configuration.getParameter("literals-bnodes-id-cache-size", Integer.valueOf(50000)), // configuration.getParameter("literals-bnodes-value-cache-size", Integer.valueOf(50000)), // true))), // configuration.getParameter("known-uris-id-cache-size", Integer.valueOf(2000)), // configuration.getParameter("known-uris-value-cache-size", Integer.valueOf(2000)), // true); try { dictionary.initialise(this); } catch (InitialisationException e) { throw new RuntimeException(e); } }
private Builder populateSettings(Builder builder, Map<String, String> properties) throws DataServiceFault { String serversParam = properties.get(DBConstants.Cassandra.CASSANDRA_SERVERS); String[] servers = serversParam.split(","); for (String server : servers) { builder = builder.addContactPoint(server); } String portProp = properties.get(DBConstants.Cassandra.PORT); if (portProp != null) { builder = builder.withPort(Integer.parseInt(portProp)); } String clusterNameProp = properties.get(DBConstants.Cassandra.CLUSTER_NAME); if (clusterNameProp != null) { builder = builder.withClusterName(clusterNameProp); } String compressionProp = properties.get(DBConstants.Cassandra.COMPRESSION); if (compressionProp != null) { builder = builder.withCompression(Compression.valueOf(compressionProp)); } builder = this.populateCredentials(properties, builder); builder = this.populateLoadBalancingProp(properties, builder); String enableJMXProp = properties.get(DBConstants.Cassandra.ENABLE_JMX_REPORTING); if (enableJMXProp != null) { if (!Boolean.parseBoolean(enableJMXProp)) { builder = builder.withoutJMXReporting(); } } String enableMetricsProp = properties.get(DBConstants.Cassandra.ENABLE_METRICS); if (enableMetricsProp != null) { if (!Boolean.parseBoolean(enableMetricsProp)) { builder = builder.withoutMetrics(); } } builder = this.populatePoolingSettings(properties, builder); String versionProp = properties.get(DBConstants.Cassandra.PROTOCOL_VERSION); if (versionProp != null) { builder = builder.withProtocolVersion(ProtocolVersion.fromInt(Integer.parseInt(versionProp))); } builder = this.populateQueryOptions(properties, builder); builder = this.populateReconnectPolicy(properties, builder); builder = this.populateRetrytPolicy(properties, builder); builder = this.populateSocketOptions(properties, builder); String enableSSLProp = properties.get(DBConstants.Cassandra.ENABLE_SSL); if (enableSSLProp != null) { if (Boolean.parseBoolean(enableSSLProp)) { builder = builder.withSSL(); } } return builder; }
public Compression getCompression() { return compression; }