Search in sources :

Example 1 with CreateKeyspaceSpecification

use of org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification in project spring-data-cassandra by spring-projects.

the class KeyspaceActionSpecificationFactory method create.

/**
 * Generate a {@link CreateKeyspaceSpecification} for the keyspace.
 *
 * @param ifNotExists {@literal true} to include {@code IF NOT EXISTS} rendering in the create statement.
 * @return the {@link CreateKeyspaceSpecification}.
 */
public CreateKeyspaceSpecification create(boolean ifNotExists) {
    CreateKeyspaceSpecification create = CreateKeyspaceSpecification.createKeyspace(name).ifNotExists(ifNotExists).with(KeyspaceOption.DURABLE_WRITES, durableWrites);
    Map<Option, Object> replication = getReplication();
    if (!replication.isEmpty()) {
        create.with(KeyspaceOption.REPLICATION, replication);
    }
    return create;
}
Also used : CreateKeyspaceSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification) DefaultOption(org.springframework.data.cassandra.core.cql.keyspace.DefaultOption) KeyspaceOption(org.springframework.data.cassandra.core.cql.keyspace.KeyspaceOption) Option(org.springframework.data.cassandra.core.cql.keyspace.Option)

Example 2 with CreateKeyspaceSpecification

use of org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification in project stream-applications by spring-cloud.

the class CassandraAppClusterConfiguration method keyspaceCreator.

@Bean
@ConditionalOnProperty("cassandra.cluster.create-keyspace")
public Object keyspaceCreator(CassandraProperties cassandraProperties, CqlSessionBuilder cqlSessionBuilder) {
    CreateKeyspaceSpecification createKeyspaceSpecification = CreateKeyspaceSpecification.createKeyspace(cassandraProperties.getKeyspaceName()).withSimpleReplication().ifNotExists();
    String createKeySpaceQuery = new CreateKeyspaceCqlGenerator(createKeyspaceSpecification).toCql();
    CqlSession systemSession = cqlSessionBuilder.withKeyspace(CqlSessionFactoryBean.CASSANDRA_SYSTEM_SESSION).build();
    CqlTemplate template = new CqlTemplate(systemSession);
    template.execute(createKeySpaceQuery);
    return null;
}
Also used : CqlTemplate(org.springframework.data.cassandra.core.cql.CqlTemplate) CreateKeyspaceSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CreateKeyspaceCqlGenerator(org.springframework.data.cassandra.core.cql.generator.CreateKeyspaceCqlGenerator) Bean(org.springframework.context.annotation.Bean) CqlSessionFactoryBean(org.springframework.data.cassandra.config.CqlSessionFactoryBean) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)

Example 3 with CreateKeyspaceSpecification

use of org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification in project spring-data-cassandra by spring-projects.

the class CassandraOperationsProducer method createCassandraOperations.

@Produces
@ApplicationScoped
public CassandraOperations createCassandraOperations(CqlSession session) throws Exception {
    CassandraMappingContext mappingContext = new CassandraMappingContext();
    mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(session, CqlIdentifier.fromCql(KEYSPACE_NAME)));
    mappingContext.setInitialEntitySet(Collections.singleton(User.class));
    mappingContext.afterPropertiesSet();
    MappingCassandraConverter cassandraConverter = new MappingCassandraConverter(mappingContext);
    CassandraAdminTemplate cassandraTemplate = new CassandraAdminTemplate(session, cassandraConverter);
    CreateKeyspaceSpecification createKeyspaceSpecification = CreateKeyspaceSpecification.createKeyspace(KEYSPACE_NAME).ifNotExists();
    cassandraTemplate.getCqlOperations().execute(CreateKeyspaceCqlGenerator.toCql(createKeyspaceSpecification));
    cassandraTemplate.getCqlOperations().execute("USE " + KEYSPACE_NAME);
    CassandraPersistentEntitySchemaDropper schemaDropper = new CassandraPersistentEntitySchemaDropper(mappingContext, cassandraTemplate);
    schemaDropper.dropTables(false);
    schemaDropper.dropUserTypes(false);
    CassandraPersistentEntitySchemaCreator schemaCreator = new CassandraPersistentEntitySchemaCreator(mappingContext, cassandraTemplate);
    schemaCreator.createUserTypes(false);
    schemaCreator.createTables(false);
    for (CassandraPersistentEntity<?> entity : cassandraTemplate.getConverter().getMappingContext().getTableEntities()) {
        cassandraTemplate.truncate(entity.getType());
    }
    return cassandraTemplate;
}
Also used : CassandraPersistentEntitySchemaDropper(org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaDropper) User(org.springframework.data.cassandra.domain.User) CassandraAdminTemplate(org.springframework.data.cassandra.core.CassandraAdminTemplate) CreateKeyspaceSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification) SimpleUserTypeResolver(org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver) CassandraPersistentEntitySchemaCreator(org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaCreator) CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) MappingCassandraConverter(org.springframework.data.cassandra.core.convert.MappingCassandraConverter) Produces(javax.enterprise.inject.Produces) ApplicationScoped(javax.enterprise.context.ApplicationScoped)

Example 4 with CreateKeyspaceSpecification

use of org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification in project spring-data-cassandra by spring-projects.

the class KeyspaceActionSpecificationFactoryBeanUnitTests method shouldCreateKeyspace.

// DATACASS-502
@Test
void shouldCreateKeyspace() {
    bean.setAction(KeyspaceAction.CREATE);
    bean.setName("my_keyspace");
    bean.setReplicationStrategy(ReplicationStrategy.SIMPLE_STRATEGY);
    bean.setReplicationFactor(1);
    bean.afterPropertiesSet();
    List<KeyspaceActionSpecification> actions = bean.getObject().getActions();
    assertThat(actions).hasSize(1).hasAtLeastOneElementOfType(CreateKeyspaceSpecification.class);
    CreateKeyspaceSpecification create = (CreateKeyspaceSpecification) actions.get(0);
    assertThat(create.getName()).isEqualTo(CqlIdentifier.fromCql("my_keyspace"));
    assertThat(create.getOptions()).containsKeys("durable_writes", "replication");
}
Also used : KeyspaceActionSpecification(org.springframework.data.cassandra.core.cql.keyspace.KeyspaceActionSpecification) CreateKeyspaceSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification) Test(org.junit.jupiter.api.Test)

Aggregations

CreateKeyspaceSpecification (org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification)4 CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Produces (javax.enterprise.inject.Produces)1 Test (org.junit.jupiter.api.Test)1 ConditionalOnProperty (org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)1 Bean (org.springframework.context.annotation.Bean)1 CqlSessionFactoryBean (org.springframework.data.cassandra.config.CqlSessionFactoryBean)1 CassandraAdminTemplate (org.springframework.data.cassandra.core.CassandraAdminTemplate)1 CassandraPersistentEntitySchemaCreator (org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaCreator)1 CassandraPersistentEntitySchemaDropper (org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaDropper)1 MappingCassandraConverter (org.springframework.data.cassandra.core.convert.MappingCassandraConverter)1 CqlTemplate (org.springframework.data.cassandra.core.cql.CqlTemplate)1 CreateKeyspaceCqlGenerator (org.springframework.data.cassandra.core.cql.generator.CreateKeyspaceCqlGenerator)1 DefaultOption (org.springframework.data.cassandra.core.cql.keyspace.DefaultOption)1 KeyspaceActionSpecification (org.springframework.data.cassandra.core.cql.keyspace.KeyspaceActionSpecification)1 KeyspaceOption (org.springframework.data.cassandra.core.cql.keyspace.KeyspaceOption)1 Option (org.springframework.data.cassandra.core.cql.keyspace.Option)1 CassandraMappingContext (org.springframework.data.cassandra.core.mapping.CassandraMappingContext)1 SimpleUserTypeResolver (org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver)1