Search in sources :

Example 1 with CassandraAdminTemplate

use of org.springframework.data.cassandra.core.CassandraAdminTemplate in project spring-data-cassandra by spring-projects.

the class SessionFactoryFactoryBean method performSchemaActions.

@SuppressWarnings("all")
private void performSchemaActions(boolean drop, boolean dropUnused, boolean ifNotExists) throws Exception {
    CassandraAdminOperations adminOperations = new CassandraAdminTemplate(getObject(), this.converter);
    CassandraPersistentEntitySchemaCreator schemaCreator = new CassandraPersistentEntitySchemaCreator(this.converter.getMappingContext(), adminOperations);
    if (drop) {
        CassandraPersistentEntitySchemaDropper schemaDropper = new CassandraPersistentEntitySchemaDropper(this.converter.getMappingContext(), adminOperations);
        schemaDropper.dropTables(dropUnused);
        schemaDropper.dropUserTypes(dropUnused);
    }
    schemaCreator.createUserTypes(ifNotExists);
    schemaCreator.createTables(ifNotExists);
    schemaCreator.createIndexes(ifNotExists);
}
Also used : CassandraPersistentEntitySchemaDropper(org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaDropper) CassandraAdminOperations(org.springframework.data.cassandra.core.CassandraAdminOperations) CassandraAdminTemplate(org.springframework.data.cassandra.core.CassandraAdminTemplate) CassandraPersistentEntitySchemaCreator(org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaCreator)

Example 2 with CassandraAdminTemplate

use of org.springframework.data.cassandra.core.CassandraAdminTemplate in project spring-data-cassandra by spring-projects.

the class CassandraTemplateProducer method createCassandraOperations.

@Produces
@ApplicationScoped
public CassandraOperations createCassandraOperations(CqlSession session) throws Exception {
    CassandraMappingContext mappingContext = new CassandraMappingContext();
    mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(session));
    mappingContext.afterPropertiesSet();
    MappingCassandraConverter cassandraConverter = new MappingCassandraConverter(mappingContext);
    cassandraConverter.afterPropertiesSet();
    return new CassandraAdminTemplate(session, cassandraConverter);
}
Also used : CassandraAdminTemplate(org.springframework.data.cassandra.core.CassandraAdminTemplate) SimpleUserTypeResolver(org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver) 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 3 with CassandraAdminTemplate

use of org.springframework.data.cassandra.core.CassandraAdminTemplate 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 CassandraAdminTemplate

use of org.springframework.data.cassandra.core.CassandraAdminTemplate in project spring-data-cassandra by spring-projects.

the class CqlSessionFactoryBean method createTables.

/**
 * Perform schema actions.
 *
 * @param drop {@literal true} to drop types/tables.
 * @param dropUnused {@literal true} to drop unused types/tables (i.e. types/tables not known to be used by the
 *          {@link CassandraMappingContext}).
 * @param ifNotExists {@literal true} to perform fail-safe creations by adding {@code IF NOT EXISTS} to each creation
 *          statement.
 */
protected void createTables(boolean drop, boolean dropUnused, boolean ifNotExists) {
    CassandraAdminTemplate adminTemplate = new CassandraAdminTemplate(this.session, this.converter);
    performSchemaActions(drop, dropUnused, ifNotExists, adminTemplate);
}
Also used : CassandraAdminTemplate(org.springframework.data.cassandra.core.CassandraAdminTemplate)

Aggregations

CassandraAdminTemplate (org.springframework.data.cassandra.core.CassandraAdminTemplate)4 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 Produces (javax.enterprise.inject.Produces)2 CassandraPersistentEntitySchemaCreator (org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaCreator)2 CassandraPersistentEntitySchemaDropper (org.springframework.data.cassandra.core.CassandraPersistentEntitySchemaDropper)2 MappingCassandraConverter (org.springframework.data.cassandra.core.convert.MappingCassandraConverter)2 CassandraMappingContext (org.springframework.data.cassandra.core.mapping.CassandraMappingContext)2 SimpleUserTypeResolver (org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver)2 CassandraAdminOperations (org.springframework.data.cassandra.core.CassandraAdminOperations)1 CreateKeyspaceSpecification (org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification)1 User (org.springframework.data.cassandra.domain.User)1