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;
}
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;
}
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;
}
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");
}
Aggregations