Search in sources :

Example 1 with CassandraBuilder

use of com.github.nosan.embedded.cassandra.CassandraBuilder in project embedded-cassandra by nosan.

the class CassandraExamples method quickStart.

private void quickStart() {
    // tag::quick-start[]
    Cassandra cassandra = new CassandraBuilder().build();
    cassandra.start();
    try {
        Settings settings = cassandra.getSettings();
        try (CqlSession session = CqlSession.builder().addContactPoint(new InetSocketAddress(settings.getAddress(), settings.getPort())).withLocalDatacenter("datacenter1").build()) {
            CqlScript.ofClassPath("schema.cql").forEachStatement(session::execute);
        }
    } finally {
        cassandra.stop();
    }
// end::quick-start[]
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Cassandra(com.github.nosan.embedded.cassandra.Cassandra) CassandraBuilder(com.github.nosan.embedded.cassandra.CassandraBuilder) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Settings(com.github.nosan.embedded.cassandra.Settings)

Example 2 with CassandraBuilder

use of com.github.nosan.embedded.cassandra.CassandraBuilder in project embedded-cassandra by nosan.

the class CassandraExamples method workingDirectoryInitializer.

// end::start-shared-cassandra[]
private void workingDirectoryInitializer() {
    // tag::working-directory-initializer[]
    new CassandraBuilder().workingDirectoryInitializer(new WorkingDirectoryInitializer() {

        @Override
        public void init(Path workingDirectory, Version version) throws IOException {
        // Custom logic
        }
    }).build();
    // end::working-directory-initializer[]
    // tag::working-directory-initializer-skip-existing[]
    new CassandraBuilder().workingDirectoryInitializer(new DefaultWorkingDirectoryInitializer(new WebCassandraDirectoryProvider(), DefaultWorkingDirectoryInitializer.CopyStrategy.SKIP_EXISTING)).build();
// end::working-directory-initializer-skip-existing[]
}
Also used : Path(java.nio.file.Path) WebCassandraDirectoryProvider(com.github.nosan.embedded.cassandra.WebCassandraDirectoryProvider) Version(com.github.nosan.embedded.cassandra.Version) CassandraBuilder(com.github.nosan.embedded.cassandra.CassandraBuilder) DefaultWorkingDirectoryInitializer(com.github.nosan.embedded.cassandra.DefaultWorkingDirectoryInitializer) WorkingDirectoryInitializer(com.github.nosan.embedded.cassandra.WorkingDirectoryInitializer) DefaultWorkingDirectoryInitializer(com.github.nosan.embedded.cassandra.DefaultWorkingDirectoryInitializer)

Example 3 with CassandraBuilder

use of com.github.nosan.embedded.cassandra.CassandraBuilder in project esop by instaclustr.

the class AbstractBackupTest method getCassandra.

protected Cassandra getCassandra(final Path cassandraHome, final String version, final WorkingDirectoryCustomizer customizer) throws Exception {
    FileUtils.createDirectory(cassandraHome);
    CassandraBuilder builder = new CassandraBuilder();
    builder.version(Version.parse(version));
    builder.jvmOptions("-Dcassandra.ring_delay_ms=1000", "-Xms1g", "-Xmx1g");
    builder.workingDirectory(() -> cassandraHome);
    builder.addConfigProperties(new HashMap<String, Object>() {

        {
            put("data_file_directories", new ArrayList<String>() {

                {
                    add(cassandraHome.toAbsolutePath() + "/data/data");
                    add(cassandraHome.toAbsolutePath() + "/data/data2");
                    add(cassandraHome.toAbsolutePath() + "/data/data3");
                }
            });
        }
    });
    if (customizer != null) {
        builder.workingDirectoryCustomizers(customizer);
    }
    if (version.startsWith("2")) {
        FileUtils.createDirectory(cassandraHome.resolve("data").resolve("data"));
        builder.addConfigProperties(new HashMap<String, String[]>() {

            {
                put("data_file_directories", new String[] { cassandraHome.resolve("data").resolve("data").toAbsolutePath().toString() });
            }
        });
    }
    return builder.build();
}
Also used : ArrayList(java.util.ArrayList) CassandraBuilder(com.github.nosan.embedded.cassandra.CassandraBuilder)

Example 4 with CassandraBuilder

use of com.github.nosan.embedded.cassandra.CassandraBuilder in project embedded-cassandra by nosan.

the class CassandraExamples method clientEncryptionOptions.

private void clientEncryptionOptions() {
    // tag::client-encryption-options[]
    ClassPathResource keystore = new ClassPathResource("server.keystore");
    ClassPathResource truststore = new ClassPathResource("server.truststore");
    new CassandraBuilder().addWorkingDirectoryResource(keystore, "conf/server.keystore").addWorkingDirectoryResource(truststore, "conf/server.truststore").addConfigProperty("client_encryption_options.enabled", true).addConfigProperty("client_encryption_options.require_client_auth", true).addConfigProperty("client_encryption_options.optional", false).addConfigProperty("client_encryption_options.keystore", "conf/server.keystore").addConfigProperty("client_encryption_options.truststore", "conf/server.truststore").addConfigProperty("client_encryption_options.keystore_password", "123456").addConfigProperty("client_encryption_options.truststore_password", "123456").addConfigProperty("native_transport_port_ssl", 9142).build();
// end::client-encryption-options[]
}
Also used : CassandraBuilder(com.github.nosan.embedded.cassandra.CassandraBuilder) ClassPathResource(com.github.nosan.embedded.cassandra.commons.ClassPathResource)

Example 5 with CassandraBuilder

use of com.github.nosan.embedded.cassandra.CassandraBuilder in project embedded-cassandra-spring-boot-starter by nosan.

the class EmbeddedCassandraAutoConfiguration method embeddedCassandraBuilder.

@Bean
@ConditionalOnMissingBean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
CassandraBuilder embeddedCassandraBuilder(EmbeddedCassandraProperties properties, ObjectProvider<CassandraBuilderConfigurator> configurators) throws IOException {
    PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
    CassandraBuilder builder = new CassandraBuilder();
    builder.addEnvironmentVariables(properties.getEnvironmentVariables());
    builder.addSystemProperties(properties.getSystemProperties());
    builder.addConfigProperties(properties.getConfigProperties());
    builder.addJvmOptions(properties.getJvmOptions());
    map.from(properties::getRegisterShutdownHook).to(builder::registerShutdownHook);
    map.from(properties::getVersion).whenHasText().to(builder::version);
    map.from(properties::getLogger).whenHasText().as(Logger::get).to(builder::logger);
    map.from(properties::getName).whenHasText().to(builder::name);
    map.from(properties::getWorkingDirectory).to(workingDirectory -> builder.workingDirectory(() -> workingDirectory));
    map.from(properties::getStartupTimeout).whenNot(Duration::isNegative).whenNot(Duration::isZero).to(builder::startupTimeout);
    Resource configFile = properties.getConfigFile();
    if (configFile != null) {
        builder.configFile(new UrlResource(configFile.getURL()));
    }
    for (Map.Entry<String, Resource> entry : properties.getWorkingDirectoryResources().entrySet()) {
        builder.addWorkingDirectoryResource(new UrlResource(entry.getValue().getURL()), entry.getKey());
    }
    configurators.orderedStream().forEach(builder::configure);
    return builder;
}
Also used : UrlResource(com.github.nosan.embedded.cassandra.commons.UrlResource) UrlResource(com.github.nosan.embedded.cassandra.commons.UrlResource) Resource(org.springframework.core.io.Resource) CassandraBuilder(com.github.nosan.embedded.cassandra.CassandraBuilder) Duration(java.time.Duration) PropertyMapper(org.springframework.boot.context.properties.PropertyMapper) Map(java.util.Map) Scope(org.springframework.context.annotation.Scope) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

CassandraBuilder (com.github.nosan.embedded.cassandra.CassandraBuilder)5 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 Cassandra (com.github.nosan.embedded.cassandra.Cassandra)1 DefaultWorkingDirectoryInitializer (com.github.nosan.embedded.cassandra.DefaultWorkingDirectoryInitializer)1 Settings (com.github.nosan.embedded.cassandra.Settings)1 Version (com.github.nosan.embedded.cassandra.Version)1 WebCassandraDirectoryProvider (com.github.nosan.embedded.cassandra.WebCassandraDirectoryProvider)1 WorkingDirectoryInitializer (com.github.nosan.embedded.cassandra.WorkingDirectoryInitializer)1 ClassPathResource (com.github.nosan.embedded.cassandra.commons.ClassPathResource)1 UrlResource (com.github.nosan.embedded.cassandra.commons.UrlResource)1 InetSocketAddress (java.net.InetSocketAddress)1 Path (java.nio.file.Path)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 PropertyMapper (org.springframework.boot.context.properties.PropertyMapper)1 Bean (org.springframework.context.annotation.Bean)1 Scope (org.springframework.context.annotation.Scope)1 Resource (org.springframework.core.io.Resource)1