Skip to content

Instantly share code, notes, and snippets.

@JensRantil
Created August 19, 2014 09:36
Show Gist options
  • Save JensRantil/f040e900edc8a2ee6192 to your computer and use it in GitHub Desktop.
Save JensRantil/f040e900edc8a2ee6192 to your computer and use it in GitHub Desktop.
spring-data-cassandra init example
package com.project.common.config;
t
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import com.datastax.driver.core.AuthProvider;
import com.datastax.driver.core.PlainTextAuthProvider;
@Configuration
@EnableCassandraRepositories(basePackages = {
"com.project.common.repository.cassandra",
})
public class DistributedRepositoryConfiguration {
// ...
@Bean
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints(constructSeedsString(configuration.get().getSeeds()));
cluster.setPort(DEFAULT_CASSANDRA_CQL_PORT);
// ...
try {
cluster.afterPropertiesSet();
} catch (Exception e) {
log.error("Could not initiate distributed storage connector.", e);
throw new RuntimeException("Could not initiate distributed storage connector.", e);
}
return cluster;
}
@Bean
public CassandraMappingContext mappingContext() {
return new BasicCassandraMappingContext();
}
@Bean
public CassandraConverter converter() {
return new MappingCassandraConverter(mappingContext());
}
@Bean
public CassandraSessionFactoryBean session() throws Exception {
CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
session.setCluster(cluster().getObject());
session.setKeyspaceName("mykeyspace");
session.setConverter(converter());
session.setSchemaAction(SchemaAction.NONE);
session.afterPropertiesSet();
return session;
}
@Bean
public CassandraOperations cassandraTemplate() throws Exception {
return new CassandraTemplate(session().getObject());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment